Passing by Reference

Started by
4 comments, last by bakery2k1 18 years ago
I need to pass by reference so that the object changes its value after returning. this is the code that I assume would function properly.

//print.h
void Path(  std::map<std::string, std::string>& path );
..
..

//parameter.h
std::map<std::string, std::string> GetParameter(){
	return fieldData;
};
..
..

//main
main(){
	print::Instance().Parameter( parameter.GetParameter() );
}



However, end up with a compile error. new_shenu.cpp:18: error: no matching function for call to `Print::Parameter(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >)' Print.h:37: note: candidates are: void Print::Parameter(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<const std::string, std::string> > >&) Why? well, I've seen some people use the const keyword, so I tried

//print.h
void Path( const std::map<std::string, std::string>& path );
..
..

and i was successful in compiling but I know then I won't be able to change the data.
Advertisement
Well, it's a little hard to tell where the error occurs because you aren't calling the problem function in the code snipet. It would help if we could see the prototype, definition, and usage of the function.

It's a little off-topic, but for your own sake (and ours), use some typedefs - it makes it much easier to understand the code:

// ah-h-h-h, sweet, sweet typedef...typedef std::map< std::string, std::string > StringStringMap;// much better :)void Path( StringStringMap &path );StringStringMap GetParameter(){    return fieldData;}...
You never call print in your example code, so there's no way changing the definition of path will solve anything.

The problem is likely that you have a const path at some point. That path can't be sent to a non-const reference parameter, so the compiler tries to pass it by value and fails. When you add a const reference version, that matches up OK and everything compiles.

That doesn't seem like the right error message, but without the actual code this is my best guess.

CM
// singleton.h#ifndef SINGLETON#define SINGLETONtemplate <class T>class Singleton{	public:		static T& Instance() {			static T _instance;			return _instance;		}	private:		Singleton();          // ctor hidden		~Singleton();          // dtor hidden		Singleton(Singleton const&);    // copy ctor hidden		Singleton& operator=(Singleton const&);  // assign op hidden};#endif// main.cpp#include "Singleton.h"#include "Cookie.h"#include "Print.h"#include "Parameter.h"#include "Path.h"......typedef Singleton<Print> print;int main(){	//Print print;	Cookie cookie;	Parameter parameter;	Path path;	parameter.SetParameter();	path.SetPath();	print::Instance().Parameter( parameter.GetParameter() ); // <-- problem	print::Instance().Cookie( cookie.GetCookie() ); // <-- problem	print::Instance().Path( path.GetPath() ); // <-- problem	if ( parameter.TestKey("db") ){		if (  parameter.TestKeyType("action") == "input" ){			//input action		}		else if (  parameter.TestKeyType("action") == "delete" ){			//delete action		}		else{			//llist action		}	}	else{		print::Instance().Error("no_db");	}	return 0;}//print.hclass Print {void Parameter( const std::map<std::string, std::string>& fieldData );	//end public:};//parameter.hclass Parameter{	public:		Parameter();		std::map<std::string, std::string> GetParameter(){			return fieldData;		};};#endif
I also get these kind of compile errors:
151# g++ -o new_shenu.cgi new_shenu.cpp Algorithms.cpp Path.cpp Cookie.cpp Parameter.cpp
/var/tmp//ccmKyJcL.o(.text+0x197): In function `main':
: undefined reference to `Print::Parameter(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
/var/tmp//ccmKyJcL.o(.text+0x205): In function `main':
: undefined reference to `Print::Cookie(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
/var/tmp//ccmKyJcL.o(.text+0x276): In function `main':
: undefined reference to `Print::Path(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
/var/tmp//ccmKyJcL.o(.text+0x624): In function `main':
: undefined reference to `Print::Error(std::string)'
/var/tmp//ccmKyJcL.o(.gnu.linkonce.t._ZN9SingletonI5PrintE8InstanceEv+0x18): In function `Singleton<Print>::Instance()':
: undefined reference to `Print::Print()'
/var/tmp//ccJ2h5hH.o(.text+0x25b): In function `Algorithms::ParseData(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > >&, std::string)':
: undefined reference to `Print::Error(std::string, std::string)'
151#

and I believe this has to do with the singleton implementation.
The problem in your OP is that you are trying to pass a temporary object (the return value of GetParameter) into a function taking a reference. This is not allowed, precisely to avoid the situation you now have.

I assume that Print::Parameter changes the value of the map passed in? In that case, if your code was allowed to compile, it would be modifying the temporary value which was returned from parameter.GetParameter(). What you need to do is make GetParameter() return a reference, so that Print::Parameter modifies fieldData.

This topic is closed to new replies.

Advertisement