Why my references(&) are not compiled on the iphone (xcode)?
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.
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.
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.
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.
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.
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.
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?
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).
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.
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:
This will print:
const reference to int: 10
reference to int: 3
const reference to int: 54
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;
}const reference to int: 10
reference to int: 3
const reference to int: 54
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| installing app via xcode to iphone 4s, cause iphone to freeze while calling or receiv | sefiroths | 3 | 3,352 |
Feb 11, 2012 12:10 PM Last Post: sefiroths |
|
| Xcode 4 Build Failure on iPhone target | SparkyNZ | 2 | 5,986 |
Apr 7, 2011 02:48 AM Last Post: SparkyNZ |
|
| Updating the iPhone SDK/Xcode | the_wandering_monster | 3 | 3,163 |
Dec 12, 2009 06:54 PM Last Post: Gillissie |
|
| Xcode resource groups and folder references when building for iPhone | reubert | 2 | 3,928 |
May 7, 2009 09:35 AM Last Post: reubert |
|
| How can I avoid XCODE transform my PNGs when moved to iPhone? | riruilo | 7 | 4,950 |
Apr 24, 2009 12:02 AM Last Post: smallstepforman |
|

