qream
qream

Reputation: 71

Compile error: base operand of ‘->’ has non-pointer type ‘Token’

I get the error mentioned in the title when trying to compile my c++ code. I'm having trouble understanding what I did wrong here.

The compiler has a problem with my implementation of the bool operator==(Token ) function. I thought this was the way to overload an operator.

Any clues as to why the compiler doesn't like me referring to this->terminal or this->lexeme?

class Token {
    public:
        tokenType terminal;
        std::string lexeme;
        Token *next;

        Token();
        bool operator==(Token &t);
    private:
        int lexemelength, line, column;
};

bool Token::operator==(Token &t) {
    return ((this->terminal == t->terminal) &&
            (this->lexeme == t->lexeme));
}

Upvotes: 7

Views: 18224

Answers (1)

Take a close look at your types. t is a reference (Token &t) meaning it must be referred to using the dot operator (.).

References are not pointers; think of them as already dereferenced pointers without putting the actual object on the stack (passing "by reference").

Upvotes: 12

Related Questions