错误C2679:二进制'<<':找不到带有' std :: string'类型的右手操作数的运算符

时间:2015-11-28 14:33:20

标签: c++ operator-overloading

我有一个简单的书籍链表,我试图用这种方法打印链表的内容

void List::display()const{

    Node *newNode = head;

    while (newNode != NULL){
        cout << "ID: " << newNode->book.getId() << " Name: " << newNode->book.getName()<< endl;
        newNode = newNode->next;
    }
}

我的Book类具有以下实现:

int Book::getId(){
    return id;
}

string Book::getName(){
    return name;
}

Book.h具有以下声明:

class Book{
    friend class Node;
    friend ostream& operator<<(ostream& output, const Book &book);
public:
    Book();
    int getId();
    string getName();

private:
    int id;
    string name;
};

让它打印本书的ID很好:

cout << "ID: " << newNode->book.getId()

它的第二部分不起作用:

cout<<" Name: " << newNode->book.getName()<< endl;

我之前在几个不同的链接列表中试过这个并且它运行正常,但我无法弄清楚这里有什么问题,

错误是:

  

错误1错误C2679:二进制&#39;&lt;&lt;&#39; :找不到哪个运营商需要   类型&#39; std :: string&#39;的右侧操作数(或者没有可接受的   转化率)

1 个答案:

答案 0 :(得分:2)

你可能忘了:

#include <string>