【问题标题】:error C2679: binary '<<':no operator found which takes a right-hand operand of type 'std::string'错误 C2679:二进制“<<”:未找到采用“std::string”类型的右侧操作数的运算符
【发布时间】:2015-11-28 14:33:19
【问题描述】:

我有一个简单的书籍链接列表,我正在尝试使用此方法打印链接列表的内容

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: 二进制 '

【问题讨论】:

标签: c++ operator-overloading


【解决方案1】:

你可能忘记了:

#include <string>

【讨论】:

  • @JohnQ321 不要感到孤单! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多