从构建源代码我得到了这个错误,我不确定它在我之前的帖子中意味着什么,我得到了帮助,而且我得到了更多,这就是我现在所拥有的,感谢您的帮助,
main.cpp:31:2:错误:不匹配'operator<<'在'os<< (& q) - > Queue :: PrintQ()'
template<class T> class Queue;
template<class T> ostream& operator<<(ostream& os, Queue<T>& q);
template<class T>
class Queue{
protected:
vector<T> theQ;
public:
void Push(T item);
T pop();
void ReadAnItem();
void PrintQ();
friend ostream& operator<< <T>(ostream& os, Queue<T>& q);
};
template<class T>
ostream& operator<< (ostream& os, Queue<T>& q){
os << q.PrintQ();
return os;
}
template<class T>
void Queue<T>::Push(T item){
theQ.push_back(item);
}
template<class T>
T pop(){
}
template<class T>
void Queue<T>::ReadAnItem(){
T item;
cout << "Enter the data please: " << endl;
cin >> item;
Push(item);
}
template<class T>
void Queue<T>::PrintQ(){
cout << "The content of the array is as follows: " << endl;
for (int i=0; i < theQ.size(); i++){
cout << theQ[i];
cout<< endl;
}
}
class Employee{
protected:
long empId;
string empName;
string email;
public:
Employee(){}
Employee(long i, string n){
empName = n,
empId =i;
email = "Unknown";
}
friend ostream& operator<<(ostream& os, Employee& e){
os << e;
return os;
}
};
class Student{
protected:
long stId;
int year;
string email;
string schoolName;
public:
Student(){}
Student(long i, int y, string sn){
stId = i;
year =y;
email = "Unknown";
schoolName=sn;
}
friend ostream& operator<<(ostream& os, Student& s){
os << s;
return os;
}
};
int main(){
Queue<Student> s;
s.Push(Student(300982, 21, "Andrey"));
cout << s;
return 0;
}
答案 0 :(得分:1)
这是一个问题:
os << q.PrintQ();
PrintQ()
是void
。您正尝试将对void
函数的调用结果流式传输到std::cout
。可能的解决方案如下:
template<class T>
void Queue<T>::PrintQ(std::ostream& o) const {
o << "The content of the array is as follows: " << endl;
for (int i=0; i < theQ.size(); i++){
o << theQ[i];
o << endl;
}
}
然后
template<class T>
ostream& operator<< (ostream& os, const Queue<T>& q){
q.PrintQ(os);
return os;
}
答案 1 :(得分:0)
一些小问题:
最好是STL并使用算法:
copy( theQ.begin(), theQ.end(), ostream_iterator<T&>(os, "\n"));
os << s
来解决这个问题
像os << "Id: " << s.stId << "\tYear: " << s.year << "\temail: "
<< s.email << "\tschoolname: " << s.schoolName;
你也可以写一个
流成员函数并保留os << s;
行。希望这有任何帮助。
祝你好运阿尔文