user2044757
user2044757

Reputation: 19

An element of a pointer vector of object pointers assignment

I'm having trouble assigning an element of a pointer vector of object pointers to an object pointer. I'm on a Linux and using Eclipse IDE. If you want to take a look at my problem:

void Parse::parseDatalogProgram(vector<Token*>* tokens)
{
    Token* currentToken = tokens[0];
...
}

I'm getting a syntax error saying "cannot convert ‘std::vector’ to ‘Token*’ in initialization", although above doesn't seem to look that way How do I properly fix this?

Upvotes: 1

Views: 348

Answers (3)

Rafał Rawicki
Rafał Rawicki

Reputation: 22690

tokens is a pointer to vector, not the object or a reference. The C++ way would be changing the function prototype to:

void Parse::parseDatalogProgram(const vector<Token*>& tokens){...}

If that is not possible, you can simply dereference the pointer before using it: Token* currentToken = (*tokens)[0]

Upvotes: 2

Marius Bancila
Marius Bancila

Reputation: 16328

Token* currentToken = (*tokens)[0];

Make sure tokens is not null. Why don't you pass a reference, instead?

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258588

tokens is a pointer, not a vector itself. You can do

tokens->operator[](0);

or

(*tokens)[0];

Upvotes: 5

Related Questions