Megan
Megan

Reputation: 69

no matching function call with two types of strings

my driver is calling a find function with a remove function like this:

remove(find(p, string("Hog")));

my find function is :

Node <T> * find(Node <T> * & pHead, T & e)

and my remove function is:

Node <T> * remove(const Node <T> * pRemove)

the error is saying that there is no matching function call between:

find(Node >*&, std::__cxx11::string) (what the driver is calling)

and

find(Node*&, T&) [with T = std::__cxx11::basic_string] (what my find function is using)

the only difference I can see is for the string data the driver is using: std::__cxx11::string and mine just has std::__cxx11::basic_string<char>.

I don't know what the difference between these two is. any thoughts on how to get this function call matching?

Upvotes: 3

Views: 85

Answers (1)

Oblivion
Oblivion

Reputation: 7374

You cannot bind a temporary to a reference because typically the temporary dies immediately and a reference to it dangles. In the call it survives untill return though. You can solve the problem by changing the function to:

Node <T> * find(Node <T> * & pHead, const T & e)
                                    ^^^^^

const & prolongs the lifetime of the temporary until the return of function.

The alternative solution would be to not to use a temporary:

string s("Hog")
remove(find(p, s));

Upvotes: 4

Related Questions