C++ Pointer question

This topic is 4883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Having a problem with pointers here. say I have a function int Func( int* &pInteger ) { } and I call it from this code: ... Func( someObj->GetInt() ); ... the GetInt() functions is: SomeClass::GetInt() { return pointerToAnInt; } I was accessing the value returned by GetInt directly before, I basically made the value that GetInt() returns public instead of private. Now I get the error: c:\App\Main.cpp(352): error C2664: 'Func' : cannot convert parameter 1 from 'int *' to 'int *& ' A reference that is not to 'const' cannot be bound to a non-lvalue Is there a work around for this problem?

Share on other sites
either do this:

int Func( int* pInteger ){}

or do this:
Func( &someObj->GetInt() );

probably you want the former unless Func is changing where the pointer points to.

-me

[EDIT: actually i think the latter won't work. if you really want Func to be able to change where the pointer points make is thusly:

int Func( int* pInteger ){}

and make GetInt return an int **

Share on other sites
You can't have a non-const reference to a temporary value.

Share on other sites
Your Func function has it's parameter declared as an "int *&". This means that it wants to be able to modify what pInteger points to (not the value it points to).

However you are passing it a temporary return value which is returned from the GetInt function. Since the return value is only a temporary it can't be modified.

You probably meanint Func(int *pInteger)

or

int temp = someObj->GetInt();
Func(temp);Most probably the first one. i.e. your definition is wrong, take out the &.

Share on other sites
Your Func function has it's parameter declared as an "int *&". This means that it wants to be able to modify what pInteger points to (not the value it points to).

However you are passing it a temporary return value which is returned from the GetInt function. Since the return value is only a temporary it can't be modified.

You probably mean
int Func(int *pInteger)orint temp = someObj->GetInt();Func(temp);
Most probably the first one. i.e. your definition is wrong, take out the &.

Share on other sites
Okay, I just found a bug, the two previous posts were both me, only the first post gave me an error. There was NO MODIFICATION between the two posts, it was the exact same text and it worked the second time.
In fact it looks like it'll let me delete the post too, although it says "Anonymous Poster" WEIRD!