By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,300 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

std::map copy constructor problem

P: n/a
I have a map in a class and when I compile I get an error in the
overloaded assignment operator but not in the copy constructor:

typedef std::map<wxString, const void*> MethodMap;
MethodMap mMethods;
MethodMap hePlugin::GetAllMethods(){return mMethods;}

// This works...
hePlugin::hePlugin(const hePlugin& plugin) : mMethods(plugin.GetAllMethods()){
}

// This doesn't...
const hePlugin hePlugin::operator = (const hePlugin& plugin){
mMethods(plugin.GetAllMethods()); <== error
return *this;
}

The error I get when compiling (gcc 3.2.2 on RedHat 9) is:

no match for call to `(MethodMap) (MethodMap)`

Thanks in advance for any help!

--
Benny

(Remove your_rose_colored_glasses to mail me)

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Benny Hill wrote:
I have a map in a class and when I compile I get an error in the
overloaded assignment operator but not in the copy constructor:

typedef std::map<wxString, const void*> MethodMap;
MethodMap mMethods;
MethodMap hePlugin::GetAllMethods(){return mMethods;}
In this code you are using the copy constructor for MethodMap // This works...
hePlugin::hePlugin(const hePlugin& plugin) : mMethods(plugin.GetAllMethods()){
}

Here you are trying to use a constructor in an already contructed varible // This doesn't...
const hePlugin hePlugin::operator = (const hePlugin& plugin){
mMethods(plugin.GetAllMethods()); <== error
return *this;
}


In order to copy stl elements, do something in the lines of:

//
mMethods.assign(plugin.GetAllMethods().begin(),plu gin.GetAllMethods().end()),
//

or if it really works for you, just do
//
mMethods = plugin.GetAllMethods();
//

By the way, if you want to make your GetAllMethods function to be a
little more efficient, return const& MethodMap instead of a copy...
Jul 22 '05 #2

P: n/a
Benny Hill wrote:
I have a map in a class and when I compile I get an error in the
overloaded assignment operator but not in the copy constructor:

typedef std::map<wxString, const void*> MethodMap;
MethodMap mMethods;
MethodMap hePlugin::GetAllMethods(){return mMethods;}
Do you really want to return a copy of the map here? Maybe a const& is
enough...
And make the method const!
// This works...
hePlugin::hePlugin(const hePlugin& plugin) : mMethods(plugin.GetAllMethods()){
}

// This doesn't...
const hePlugin hePlugin::operator = (const hePlugin& plugin){
mMethods(plugin.GetAllMethods()); <== error


Try this:

mMethods = plugin.GetAllMethods();

hth

Christoph
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.