"->"的基本操作数具有非指针类型

base operand of ‘->’ has non-pointer type

本文关键字:类型 指针 操作数 gt      更新时间:2023-10-16

我在超载<<输出操作符时遇到麻烦。错误似乎不是重载本身,而是重载内部的实现,因为同样的错误发生在value()函数中。

错误(cygwin):

DeckOfCards.cpp: In function std::ostream& deck::operator<<(std::ostream&, const deck::LinkedList&):
DeckOfCards.cpp:81:20:错误:->的基本操作数是非指针的类型const deck::LinkedList

    return os << list->value();

DeckOfCards.cpp: In成员函数void deck::DeckOfCards::value(deck::LinkedList&):
DeckOfCards.cpp:100:15:错误:->的基本操作数是非指针类型deck::LinkedList

    cout << list->getListData();

相关代码如下:

DeckOfCards.cpp

// overloaded cout operator                              
ostream& operator << (ostream& os, const LinkedList& list) {
    return os << list->value();
}

// returns all cards as a string
void DeckOfCards::value(LinkedList& list) {
    cout << list->getListData();
}

LinkedList.cpp

Node::value_type LinkedList::getListData() {
    for (current = head; current != NULL; current = current->getNext()) {
        contents += current->getData() + " ";
    }
    return contents;
}

为什么我得到这些错误?

如果左操作数既不是指针也不是重载->操作符的类,则不能使用->操作符。尝试使用.操作符,如list.value()list.getListData()