Returning Temporary Value?

Started by
14 comments, last by TDragon 18 years ago
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]
Advertisement
No, I don't know what you mean. How about a code example?
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...
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?


That's because foo() takes a char*. Make it take a const char* and all will be tickity-boo.
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 mapstd::map <std::string, std::string> config;//new_shenu.cpp:97skin.Display( mainData.Get() );//Skin.h:127void Display();//Skin.h:128void Display(std::map<std::string, std::string>& para_skinData);
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 mapstd::map <std::string, std::string> config;//new_shenu.cpp:97skin.Display( mainData.Get() );//Skin.h:127void Display();//Skin.h:128void 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]
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?
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
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.



This topic is closed to new replies.

Advertisement