我正在尝试打印出统计对象的链接列表。我有一个统计类,其构造函数包含名称、级别和 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;
}
在此先感谢...贝卡。