【问题标题】:How to print a LinkedList of objects如何打印对象的 LinkedList
【发布时间】:2013-04-16 20:54:42
【问题描述】:

我正在尝试打印出统计对象的链接列表。我有一个统计类,其构造函数包含名称、级别和 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 '&lt;&lt;' : 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;

    }

提前谢谢...贝卡。

【问题讨论】:

  • 如果您向我们展示 Stats 类(以及您希望从中打印的内容),它会更容易为您提供帮助。
  • 我刚刚在统计类中编辑过 :)

标签: c++ printing doubly-linked-list


【解决方案1】:

正如它所说:你没有为你的班级统计定义 operator

 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
  }

【讨论】:

  • 我只是把它放到一个函数中吗?我必须在任何地方传递它吗?顺便谢谢:)
  • 如果不需要访问 Stat 类的私有成员,可以在外部定义
  • 我试图把它放在我的 DLL 类中,但我得到了一堆错误:错误 1 ​​错误 C4430:缺少类型说明符 - 假定为 int。错误 3 错误 C2804:二进制 'operator
  • 你放分号了吗?操作系统
  • 不,我只是将它作为一个函数复制并粘贴到我的 DoublyLinkedList 类中......
【解决方案2】:

错误准确地说明了问题所在 - 运算符 不知道如何处理您尝试打印的类型的对象。如果你想使用这样的代码,你必须为 Stats 类重载操作符。

【讨论】:

  • 好吧,严格来说不是类中。
【解决方案3】:

根据您收到的消息,您只需声明运算符

【讨论】:

    【解决方案4】:

    您需要提供

    std::ostream & operator<<(std::ostream &os, const Stats& s)
    

    它应该是免费的功能,而不是 Stats 的成员

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签