Why my references(&) are not compiled on the iphone (xcode)?

Member
Posts: 249
Joined: 2008.10
Post: #1
Hi mates!

This works perfectly on my Visual Studio
CSprite* GetSpriteByName(std::string &name)
CSprite=new GetSpriteByName(std::string("whatever"));

However, on XCODE and iPHone I receive this message:
error: no matching function for call to CSpriteSheet::GetSpriteByName(std::string)
note: candidates are: CSprite* CSpriteSheet::GetSpriteByName(std::string&)

By the way, I receive a similar message in all my methods that pass arguments using references.
For instance when I pass vectors (I use CML) like
MouseButtonUp(cml::vector2i &position)
MouseButtonUp(cml::vector2i(34,34));


Am I missing anything? Why is it working on VS but not XCODE?

Thanks a lot for your help.
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #2
redefine your methods as:
GetSpriteByName(const std::string &name)

and it will start working.

PS.

Visual C++ lets you get away with nonstandard behavior.
C++ doesn't allow binding a temporary to a non-const reference.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #3
warmi Wrote:redefine your methods as:
GetSpriteByName(const std::string &name)

and it will start working.

PS.

Visual C++ lets you get away with nonstandard behavior.
C++ doesn't allow binding a temporary to a non-const reference.

Thanks! It works!
Can you explain it better?

Is this a rule "C++ doesn't allow binding a temporary to a non-const reference." that VS does not fulfill?

What about if I want to modify the passed class? should I use pointers?

Thanks.
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #4
riruilo Wrote:Thanks! It works!
Can you explain it better?

Is this a rule "C++ doesn't allow binding a temporary to a non-const reference." that VS does not fulfill?

What about if I want to modify the passed class? should I use pointers?

Thanks.

A temporary is something that you create when calling GetSpriteByName(" some text").

The "some text" is being turned into a temporary std::string object which you don't even have access to - it is handled by the compiler for you.

If you want to modify the object then obviously you are not going to do what you were doing because you would never have a chance to get the reference back.

Instead, you would do something like this:

std::string bla="some text";
GetSpriteByName(bla);

This is no longer a temporary - you have a valid std::string named bla which you can refer to anyway you want.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #5
warmi Wrote:A temporary is something that you create when calling GetSpriteByName(" some text").

The "some text" is being turned into a temporary std::string object which you don't even have access to - it is handled by the compiler for you.

If you want to modify the object then obviously you are not going to do what you were doing because you would never have a chance to get the reference back.

Instead, you would do something like this:

std::string bla="some text";
GetSpriteByName(bla);

This is no longer a temporary - you have a valid std::string named bla which you can refer to anyway you want.

Thanks.

std::string bla="some text";
GetSpriteByName(bla);

But let's say GetSpriteByName want to modify "bla". How can that be done if I used const?
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #6
riruilo Wrote:Thanks.

std::string bla="some text";
GetSpriteByName(bla);

But let's say GetSpriteByName want to modify "bla". How can that be done if I used const?

You could create two methods:

GetSpriteByName(const std::string &something) and another GetSpriteByName( std::string &something).
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #7
warmi Wrote:You could create two methods:

GetSpriteByName(const std::string &something) and another GetSpriteByName( std::string &something).

But the second one does not compile on XCODE (but it compiles on VisualStudio).

You said the second way is not a stantard.

What do you think?

Thanks a lot for your help.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #8
If you define both, then it will choose the one that will fit best. In this case, if you use a temporary or a const std::string, it will call the function that takes a const std::string&. Otherwise, it will call the function that takes a std::string&. To give an example, take this simple program:
Code:
void foo(int &asdf)
{
    printf("reference to int: %d\n", asdf);
}

boid foo(const int &asdf)
{
    printf("const reference to int: %d\n", asdf);
}

int main()
{
    int test = 3;
    const int constTest = 54;
    foo(10);
    foo(test);
    foo(constTest);
    return 0;
}
This will print:
const reference to int: 10
reference to int: 3
const reference to int: 54
Quote this message in a reply
Post Reply