Vijay
Vijay

Reputation: 67319

C++ statement compiler error

i have a statement in my program which does a comparision of elements of a two vectors

 if(!*(it2+3).compare(*(lines_in_file.begin())))

the compiler error i am getting is:

test_file.cpp:140: error: 'class __gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >' has no member named 'compare'

it2's type is :

vector<std::string>::iterator it2=rec_vec.begin();

lines_in_file type is :

vector<std::string> lines_in_file=split(argv[2],',');

split function declaration is :

std::vector<std::string> split(const std::string &s, char delim)

I am confused a bit.already spent a lot of time thinking. could any one please help?

Upvotes: 0

Views: 284

Answers (4)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361812

Precedence of the member access operator (.) is higher than the precedence of indirection operator (*). So your code is interpreted as:

if(!*( (it2+3).compare( *(lines_in_file.begin()) ) ))

Hence the error. (extra spaces is added for clarity)

So the fix is this:

if(! ( *(it2+3) ).compare( *(lines_in_file.begin()) ))

Upvotes: 1

Yuri Stuken
Yuri Stuken

Reputation: 13620

This happens because . operator has higher precedence than * operator. Use this:

if(!(it2+3)->compare(*(lines_in_file.begin())))

or this

if(!(*(it2+3)).compare(*(lines_in_file.begin())))

(which are equal)

Upvotes: 4

etienne
etienne

Reputation: 3206

THe * operator is applied to the result of

(it2+3).compare(*(lines_in_file.begin()))

This is not what you want. Just use ():

(*(it2+3)).compare(*(lines_in_file.begin()))

Upvotes: 1

Agustin Cautin
Agustin Cautin

Reputation: 411

The problem is that the operator "." have greater precedence that "*" so this should solve the problem.

if(!(*(it2+3)).compare(*(lines_in_file.begin())))

Upvotes: 4

Related Questions