Sign in to follow this  
Tradone

Passing by Reference

Recommended Posts

Tradone    100
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.

Share this post


Link to post
Share on other sites
extralongpants    704
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;
}

...

Share this post


Link to post
Share on other sites
Conner McCloud    1135
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

Share this post


Link to post
Share on other sites
Tradone    100

// singleton.h
#ifndef SINGLETON
#define SINGLETON

template <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.h
class Print {
void Parameter( const std::map<std::string, std::string>& fieldData ); //end public:
};

//parameter.h
class Parameter{
public:
Parameter();
std::map<std::string, std::string> GetParameter(){
return fieldData;
};
};
#endif





Share this post


Link to post
Share on other sites
Tradone    100
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.

Share this post


Link to post
Share on other sites
bakery2k1    712
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this