Nick Heiner
Nick Heiner

Reputation: 122392

C++: Syntax error C2061: Unexpected identifier

What's wrong with this line of code?

bar foo(vector ftw);

It produces

error C2061: syntax error: identifier 'vector'

Upvotes: 3

Views: 7508

Answers (5)

CB Bailey
CB Bailey

Reputation: 791361

On its own, that snippet of code has no definition of bar, vector or odp. As to why you're not getting an error about the definition of bar, I can only assume that you've taken it out of context.

I assume that it is supposed to define foo as a function, that vector names a template and that it is supposed to define a parameter called ftw but in a declaration anything that is not actually being defined needs to have been declared previously so that the compiler knows what all the other identifiers mean.

For example, if you define new types as follows you get a snippet that will compile:

struct bar {};
struct odp {};
template<class T> struct vector {};

bar foo(vector<odp> ftw);

Upvotes: 0

Alan
Alan

Reputation: 46813

Do you have:

#include <vector>

and

using namespace std; in your code?

<vector> defines the std::vector class, so you need to include it some where in your file.

since you're using vector, you need to instruct the compiler that you're going to import the whole std namespace (arguably this is not something you want to do), via using namespace std;

Otherwise vector should be defined as std::vector<myclass>

Upvotes: 1

TreDubZedd
TreDubZedd

Reputation: 2556

try std::vector<odp> or using std;

Upvotes: 0

Adrian Grigore
Adrian Grigore

Reputation: 33318

try std::vector instead. Also, make sure you

#include <vector>

Upvotes: 5

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

Probably you forgot to include vector and/or import std::vector into the namespace.

Make sure you have:

#include <vector>

Then add:

using std::vector;

or just use:

bar foo(std::vector<odp> ftw);

Upvotes: 4

Related Questions