简体   繁体   中英

error C2679: binary '<<':no operator found which takes a right-hand operand of type 'std::string'

I have a simple linked list of books and I am trying to print the contents of the linked list using this method

void List::display()const{

    Node *newNode = head;

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

My Book class has the following implementations:

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

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

The Book.h has the following declarations:

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

private:
    int id;
    string name;
};

Getting it to print the ID of the book is fine:

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

Its the second part which does not work:

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

I've tried this before in a couple of different linked lists and it works fine, but I cant figure out what's wrong here,

the error is:

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

您可能忘记了:

#include <string>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM