Jump to content
  • Advertisement
Sign in to follow this  
Mulligan

C++ Pointer question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
You can't have a non-const reference to a temporary value.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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)

or

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

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!