473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

std::map copy constructor problem

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
2 5355
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
14
by: Flzw | last post by:
Well I have a map like this : std::map <string, CObject> ObjectList; I have a function like this : CObject* NewObject( char* Name, CArg* Arg) { std::string key = Name; ObjectList =...
1
by: Bob | last post by:
Hi, I'm trying to use a map with a string key, and a pointer to objects contained in a vector. I've wrapped this in a class like so: // cMap template<class T> class cMap : public cList<T> { ...
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
7
by: Matthias Pfeifer | last post by:
Hi there, a std::map is build from std::pair elements where for a pair p p.first is called the key and p.second the value. Is there a way to keep the map sorted by the values? Ie is there a...
5
by: Chris Forone | last post by:
Hello group, g++ (3.4.2, mingw): float init = {1.f, 2.f, 3.f}; std::map<std::string, std::valarray<float mp; mp = std::valarray(init, 3); mp.size(); // should be 3, but IS 0!
6
by: Juha Nieminen | last post by:
joseph cook wrote: Not always. By default, yes, but you can specify other comparators, eg: std::map<int, int, std::greaterreversedMap;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.