Luciano Pinheiro
Luciano Pinheiro

Reputation: 95

Find() method of an std::string pointer

I have a function like this:

int find_string ( string *url){
   if(*url.find() != string::npos){
        [...]
   }
   [...]
}

And call it this way:

find_string(&url);

But I got the following compiling error:

request for member ‘find’ in ‘url’, which is of non-class type ‘std::string*’

Why this happen and, most importantly, how can I fix this? (When there was no pointer, it was working)

Upvotes: 0

Views: 96

Answers (2)

verdesmarald
verdesmarald

Reputation: 11866

*url.find() is equivalent to *(url.find()) when what you actually want is (*url).find(). Better yet you should be using url->find() instead.

The reason for this is the dereference (*) operator has lower precedence than the element selection (.) operator.

Upvotes: 3

RiaD
RiaD

Reputation: 47620

use (*url).find() or, better url->find()

*url.find tries call find() function and then use operator *

Upvotes: 3

Related Questions