为什么这段c++模版代码跑不起来,问了10几个没一个知道的
#include "stdafx.h"#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<class T1,class T2>
class people
{
public:
people();
people(const T1&x,const T2&y);
~people();
friend ostream&operator<<(ostream&x,const people&y);
private:
T1 name;
T2 age;
};
template<class T1,class T2>
people<T1,T2>::people():name("默认构造函数创建一个新对象"),age(1){}
template<class T1,class T2>
people<T1,T2>::~people(){cout << "析构people.\n";}
template<class T1,class T2>
ostream&operator<<(ostream&x,const people<T1,T2>&y)
{
x<<y.name << "的年龄:" << y.age << endl;
return(x);
}
template<class T1,class T2>
people<T1,T2>::people(const T1&x,const T2&y):name(x),age(y){}
template<class T1>
void show(const vector<T1>&x)
{
for(int i=0;i<x.size();i++)
{
cout << x << endl;
}
cout << endl;
}
int main()
{
vector<people<string,int>>one(1);
show(one);
system("pause");
}
我只能说你问的人很水 1、你定义的类成员变量Name和Age是私有的,但是重载<<符号却用了y.Name和y.Age. 这是编译不过的吧。。。
2、vector< people<string,int> >one(1); 尖括号之间加个空格吧。否则编译不过。。。
页:
[1]