Dev C 的問題,各位大大可以幫忙解決一下嗎?

我用的是Dev C++ 4.9.9.2版的,編驛的時候出現錯誤,但我不知道那裡出錯了,拜託各位大大指點一下迷津吧

#include <iostream>

#include <cstdlib>

#include <vector>

using namespace std;

#define Size 4

template <class T>

void print(vector<T> &);

int main()

{

vector<int> v;

cout << "Push back 1, 6, 4 ..." << endl;

v.push_back(1);

v.push_back(6);

v.push_back(4);

print(v);

cout << "Insert 9 ..." << endl << "v :";

v.insert(v.begin(),9);

print(v);

cout << "Erase second element ..." << endl << "v : ";

v.erase(v.begin() 1);

print(v);

cout << "Pop back ..." << endl << "v : ";

v.pop_back();

print(v);

cout << "Second element : " << v[1] << endl;

cout << "The Size of Vector : " << v.size() << endl;

cout << "Maximum size : " << v.max_size() << endl;

cout << "Using array to initialize vector ..." << endl << "v1 : ";

int a[Size] = {1,4,3,2};

vector<int> v1(a, a Size);

print(v1);

cout << "Assgin v1 to v ..." << endl << "v : ";

v = v1;

print(v);

system("PAUSE");

return 0;

}

template <class T>

void print(vector<T> & pri_con)

{

if(pri_con.empty())

{

cout << "Container is empty!" << endl;

}

else

{

vector<T>::iterator i; //在這一行出現錯誤

for(i = pri_con.begin(); i != pri_con.end(); i )

{

cout << *i << " ";

}

cout << endl;

}

}

1 Answer

Rating
  • Lv 7
    1 decade ago
    Favorite Answer

    錯誤是in function `void print(…)` expected ‘;’ before “I”in function `void print(…)` instantiated from here dependent-name ‘std::vector<...>::iterator’ is parsed as a non-type, but instantiation yields a type say ‘typename std::vector<...>::iterator’ if a type is meant其實你的g++ compiler已經告訴你要怎麼解決了啊... :)把vector<T>::iterator i;改成typename vector<T>::iterator i;就行了typename市告訴C++ compiler接下來的是個型態, 而不是變數.所以typename vector<T>::iterator i;告訴C++ compiler, vector<T>::iterator是個型態, 所以i是個vector<T>::iterator型態的變數囉.在新的C++裡, 有時你要說明你寫的vector<T>::iterator是一個型態. 不然C++ compiler會以變數處理懂不懂? 不懂請再問

Still have questions? Get your answers by asking now.