Jump to content
  • Advertisement
Sign in to follow this  
Tradone

Returning Temporary Value?

This topic is 4544 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

Q3: Returning Temporary Value? One last question about pointers, when I have a function that returns a value let's we have this method:
//mystring.h
class MyString{
public:
std::string MyString(string, string);
private:
std::string theString
}

std::string MyString::Concat(std::string para_one, std::string para_two){
theString=para_one;
theString+=para_two;
return theString;
}

//main.cpp
main(){
hello=&MyString("fooz","baz");
}


if I do something like the above I get a warning, address of a temporary value or something. why is it return such an error? and if I want the address of theString, then would I have to change the return type to a reference, as I intended? Q2:Reverse Inline? and another thing...
std::string ReturnAnswer(){
return "abc"
}
int ReturnAnswer(){
return 123;
}
void Calculate(int somenumb){
return somenumb*2
}
int main(){

Calculate(ReturnAnswer());

}


the above is sort of like the opposite of inlines I guess? b/c the compiler chooses which function ( ReturnAnswer() ) to use depending on the return type, which in the above case is an int, but an inline the compiler chooses which function depending on the input parameters not the return value. Q1:Passing References and Const passing references and the compiler complaining that there is no match, and thus forced to make it a const and later on, there is a type mismatch. do u know what i mean? how can I avoid these or am I understanding something wrong? damn, this is so aggrevating, [Edited by - Tradone on April 17, 2006 9:01:28 AM]

Share this post


Link to post
Share on other sites
Advertisement
Post code, errors or both.

I understand you have some problem relating to const and references, but I'm not going to be able to guess them...

Share this post


Link to post
Share on other sites

void foo(char *);

const char *x;
foo(x); // I believe this causes the problem

// error C2664: 'foo' : cannot convert parameter 1 from 'const char *' to 'char *'


Or is it something else?


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That's because foo() takes a char*. Make it take a const char* and all will be tickity-boo.

Share this post


Link to post
Share on other sites
sorry about that, I thought this issue was extremely general. b/c I encounter this more than half the time.

Error Messages:

new_shenu.cpp: In function 'int main(int, char**)':
new_shenu.cpp:97: error: no matching function for call to 'Skin::Display(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >)'
Skin.h:127: note: candidates are: void Skin::Display()
Skin.h:128: note: void Skin::Display(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >&)
new_shenu.cpp:123: error: no matching function for call to 'Skin::Display(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >)'
Skin.h:127: note: candidates are: void Skin::Display()
Skin.h:128: note: void Skin::Display(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >&)
*** Error code 1





//mainData is a Config type.
std::map <std::string, std::string> Config::Get(){
return config;
}
//and config is a map
std::map <std::string, std::string> config;

//new_shenu.cpp:97
skin.Display( mainData.Get() );

//Skin.h:127
void Display();

//Skin.h:128
void Display(std::map<std::string, std::string>& para_skinData);




Share this post


Link to post
Share on other sites
and this is what I tried next..

//mainData is a Config type.
std::map <std::string, std::string> Config::Get(){
return config;
}
//and config is a map
std::map <std::string, std::string> config;

//new_shenu.cpp:97
skin.Display( mainData.Get() );

//Skin.h:127
void Display();

//Skin.h:128
void Display( const std::map<std::string, std::string>& para_skinData);






New Errors:

Skin.cpp: In member function 'void Skin::Display(const std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >&)':
Skin.cpp:958: error: no matching function for call to 'Skin::Read(const char [5], const std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >&)'
Skin.cpp:172: note: candidates are: void Skin::Read(std::string)
Skin.cpp:186: note: void Skin::Read(std::string, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >&)
*** Error code 1







SOOOO ANNOYING!!!

so I follow Skin.cpp:958

void Skin::Display( const std::map<std::string, std::string>& para_skinData){
print::Instance().Header( "success" );
print::Instance().Body( "" );
Read("Head");
Read("Body", para_skinData); // this is 958
Read("Tail");
print::Instance().Tail( "" );
}





and I search the corresponding method, which was Read.

void Skin::Read( std::string para_loadKey, std::map<std::string, std::string>& para_skinData ){
skinData=NULL;
skinData=¶_skinData;
/*
std::cout << &*skinData;
for( loop=(*skinData).begin(); loop!=(*skinData).end(); loop++ ){
std::cout << loop->first << "=" << loop->second << "\n";
}
*/

if ( TestingMode )
std::cout << "**************Begin Skin::Read(std::string, std::map )****************\n\n";


Read( para_loadKey);

if ( TestingMode )
std::cout << "**************End Skin::Read(std::string, std::map )****************\n\n";

}




Now, many other classes use Read(string, map&), so I can't change Read, but I should change Display. [cry]

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
That's because foo() takes a char*. Make it take a const char* and all will be tickity-boo.


that's the thing there's so many things that depend on that method that is causing the problem. I need to fix so many things... if I do decide to make that one thing into a const. So I want to know why and when pointers and references when passed through functions are required to change to consts and such...

God.. I'm so tired.
Is it common that everybody has to go through this, or is this just me?

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus

void foo(char *);

const char *x;
foo(x); // I believe this causes the problem

// error C2664: 'foo' : cannot convert parameter 1 from 'const char *' to 'char *'


Or is it something else?


I think this is what it is, except my case is a reference.
Actually, that's another thing, I more understand pointers than references. Aren't references supposed to be easier to use and more abstract? idk about that.

If you can just briefly explain to me.... thank you so much.
you just saved me like 3 days of stress and working backwords

Share this post


Link to post
Share on other sites
Quote:
Original post by Tradone
that's the thing there's so many things that depend on that method that is causing the problem. I need to fix so many things... if I do decide to make that one thing into a const. So I want to know why and when pointers and references when passed through functions are required to change to consts and such...


Here's a nice overview of const-related cases:
http://www.parashift.com/c++-faq-lite/const-correctness.html

It gives just about all cases of where const can be used, and also what effect it has.



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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!