std::map copy constructor problem

B

Benny Hill

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::eek:perator = (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!
 
J

Jorge L Rivera

Benny said:
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::eek:perator = (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(),plugin.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...
 
C

Christoph Rabel

Benny said:
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::eek:perator = (const hePlugin& plugin){
mMethods(plugin.GetAllMethods()); <== error

Try this:

mMethods = plugin.GetAllMethods();

hth

Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top