我正在尝试打印出统计对象的链接列表。我有一个stats类,其构造函数包含name,level和exp。但我无法打印出来。这是我尝试这样做的方式:
void Print(DoublyLinkedList<Datatype> p_list)
{
int index = -1;
//Set up a new Iterator.
//DoublyLinkedListIterator<Datatype> itr = getIterator();
for(itr.Start(); itr.Valid(); itr.Forth())
{
index++;
cout <<"Index: "<< index << "\tElement: " << itr.Item() << "\n";
}
cout <<"Number of Elements in the List: " << m_count << endl;
}
这会导致itr.item()的cout错误。 错误是:
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Stats' (or there is no acceptable conversion)
这是来自doublyLinkedlist类,我在main()中设置了一个linkedList,我尝试从main()做list.print(list)。
在Stats.cpp中编辑
#include "Stats.h"
#include <string>
#include <iostream>
Validators validators2;
Stats::Stats()
{
firstName = "";
secondName = "";
level = 0;
experience = 0;
}
Stats::Stats(string firstName,string secondName, int level, int experience)
{
firstName = firstName;
secondName = secondName;
level = level;
experience = experience;
}
string Stats :: getFirstName()
{
return firstName;
}
string Stats :: getSecondName()
{
return secondName;
}
int Stats :: getLevel()
{
return level;
}
int Stats :: getExperience()
{
return experience;
}
Stats Stats :: input()
{
firstName = "Please enter the First Name: ";
string inputfirstName = validators2.getString(firstName);
secondName = "Please enter the Second Name: ";
string inputSecondName = validators2.getString(secondName);
cout<< "Please enter the level: ";
int inputLevel = validators2.getNum();
cout<< "Please enter the experience: ";
int inputExperience = validators2.getNum();
Stats s1(inputfirstName,inputSecondName,inputLevel,inputExperience);
return s1;
}
提前致谢...贝卡。
答案 0 :(得分:1)
错误说明错误 - 操作员&lt;&lt; 不知道如何处理您要打印的类型的对象。如果你想使用这样的代码,你必须重载Stats类的运算符。
答案 1 :(得分:1)
正如它所说:你没有运营商&lt;&lt;为您的班级统计数据定义。你必须定义这个:
std::ostream& operator<<(std::ostream& os, const Stats& s){
//define what it means to cout<<Stats, for example:
//print some attributes
os<<"\nfirstName: "<<s.getFirstName();
os<<"\nsecondName: "<<s.getSecondName();
os<<"\nlevel: "<<s.getLevel();
//and so on
return os; // so chunk is possible os<<a<<b<<c
}
答案 2 :(得分:0)
根据您收到的消息,您只需要声明运营商&lt;&lt;对于统计类型。
答案 3 :(得分:0)
您需要提供
std::ostream & operator<<(std::ostream &os, const Stats& s)
它应该是免费的funton,不是Stats的成员