定義這麼個模板類:
#include
using namespace std;
template
class List
{
public:
List(int MaxSize = 10);
virtual~List();
bool IsEmpty() const { return(length == 0); }
int Length() const { return length; }
bool Find(int k, T & x) const;
int Search(const T & x) const;
List& Insert(int k, const T & x);
List& Delete(int k, T & x);
void show();
private:
int length;
int MaxSize;
T *element; // 一維動態數組
};
其中:
template
void List::show(){
for (int i = 0; i < length; i++){
cout << element[i] << endl;
}
}
main函數:
void main(){
List MyList(5);
cout << "IsEmpty: " << MyList.IsEmpty() << endl;
cout << "Length: " << MyList.Length() << endl;
MyList.Insert(0, 1).Insert(1, 2).Insert(2, 3).Insert(3, 4);
cout << "MyList is: " << MyList.show() << endl;
}
然後 cout << "MyList is: " << MyList.show() << endl; 這條語句會出錯,錯誤指向 MyList.show() 前面的<< 這個符號,請問是怎麼回事???
你的show()函數類型是void,用cout輸出自然是錯的。
改為:
cout << "MyList is: " ; MyList.show();cout << endl;