首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

vector模板失误

2013-02-27 
vector模板出错#include iostream#include vectorusing namespace std templateclass type class i

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;
}

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

热点排行