Skip to main content
gamedev.net
🔒 Locked

C++ Pointer question

Started by Mulligan 4 replies 550+ views
Original Post
Mulligan
Mulligan
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?
Palidine
Palidine
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 **
iMalc
iMalc
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 &.
iMalc
iMalc
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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.