user1883329
user1883329

Reputation: 83

pass parameter in this function with reference

This is one function in .h file

LinkedListElement<char> * findLastNthElementRecursive(int n, int &current);

Try both

findLastNthElementRecursive(3,0);

and

int a = 0;
findLastNthElementRecursive(3,&a);

error is no matching function

I GOT IT findLastNthElementRecursive(3,a); SHOULE BE THIS WAY

but if I do not want to create new variable like a, how to do that?

Upvotes: 1

Views: 44

Answers (2)

Luchian Grigore
Luchian Grigore

Reputation: 258648

A temporary cannot bind to a non-const reference. In your first case, you attempt to pass a temorary as parameter, and it fails.

The second one doesn't work because &a is the address of a, effectively an int*, so doesn't match the signature of the function.

The correct way would be

int a = 0;
findLastNthElementRecursive(3,a);

Upvotes: 3

NPE
NPE

Reputation: 500883

Try:

int a = 0;
findLastNthElementRecursive(3, a);

Also note that you're ignoring the return value of findLastNthElementRecursive().

Upvotes: 1

Related Questions