读书人

vector模板失误

发布时间: 2013-02-27 10:48:11 作者: rapoo

vector模板出错

#include <iostream>
#include <vector>
using namespace std;
template<class type>
class ivector
{ public:
void tittle();

type getvector();

};

template<class type>
type ivector<type>::getvector()
{ type a,b,c;
cout<<"please input three numbers";
cin>>a>>b>>c;
vector<type>myvector;
myvector.push_back(a);
myvector.push_back(b);
myvector.push_back(c);
vector<type>::iterator it=myvector.begin();
vector<type>::iterator it2=myvector.end();

for(it!=it2;it++)
{
cout<<"the list is that"<<*it<<endl;
cout<<endl;
}

};
void ivector<type>::tittle()
{
cout<<"ready to input the numbers"<<endl;
}

int main()
{
ivector<int> dan;
dan.tittle();
dan.getvector();
system("pause");
return 0;
}



















改了很多次编译没通过 project\the virtual function.cpp `it' undeclared (first use this function

project\the virtual function.cpp expected `;' before "it2" )


32
project\the virtual function.cpp template argument 1 is invalid
[解决办法]
for()里只有一个分号。
[解决办法]
A few issues, however, this is the fixed code:

template<class T>
class ivector
{
public:
void tittle();

vector<T> getvector();

};

template<class T>
vector<T> ivector<T>::getvector()
{
T a,b,c;
cout<<"please input three numbers";
cin>>a>>b>>c;
vector<T> myvector;
myvector.push_back(a);
myvector.push_back(b);
myvector.push_back(c);
typename vector<T>::iterator it=myvector.begin(); // must add typename, why? because vector type depends on parameter type
typename vector<T>::iterator it2=myvector.end();

for(; it!=it2;it++)
{
cout<<"the list is that"<<*it<<endl;
cout<<endl;
}

return myvector;

};

template<class T> // you forgot this line
void ivector<T>::tittle()
{
cout<<"ready to input the numbers"<<endl;
}

int main()
{
ivector<int> dan;
dan.tittle();
dan.getvector();
system("pause");
return 0;
}

[解决办法]
错误信息 提示的很清楚啊。 学会看编译器的提示。

读书人网 >C++

热点推荐