473,382 Members | 1,368 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,382 software developers and data experts.

STL Map in which the value type has no default constructor

Hi,

Is it possible to have an STL map in which the value type (i.e. the
"second" of the pair) does not have an overloaded constructor?

For example, I have a map definition like this:
map<size_t, MyClass my_map;

but MyClass does not have a default constructor.

Is it possible to define the map in this way? If yes, could someone
kindly post a sample code snippet?

I am getting a linker error when I have any code that attempts to
insert into this map. As long as I leave the map alone, I am not
getting any linker error.

Thanks,
Santosh

Nov 6 '06 #1
2 8473
dr*********@gmail.com wrote:
Hi,

Is it possible to have an STL map in which the value type (i.e. the
"second" of the pair) does not have an overloaded constructor?
Nope. The C++ standard states that the value type of std::map must be
assignable (which includes that it must be copy-constructable). Also it
must be default constructable.
For example, I have a map definition like this:
map<size_t, MyClass my_map;

but MyClass does not have a default constructor.
Then define one. If your class doesn't support this semantics, you may
think about storing pointers to your class in the map.
I am getting a linker error when I have any code that attempts to
insert into this map.
You're most probably getting a compiler error, or else you have defined
the constructors, but have given no implementation.
As long as I leave the map alone, I am not
getting any linker error.
That's how templates work: you only get the code you're actually needing.

Regards,
Stuart
Nov 6 '06 #2
dr*********@gmail.com wrote:
Hi,

Is it possible to have an STL map in which the value type (i.e. the
"second" of the pair) does not have an overloaded constructor?

For example, I have a map definition like this:
map<size_t, MyClass my_map;

but MyClass does not have a default constructor.

Is it possible to define the map in this way? If yes, could someone
kindly post a sample code snippet?
#include <map>

class X {

X();

public:

X ( int i ) {}

};
int main ( void ) {
std::map< int, X m;
m.insert( std::map<int,X>::value_type( 5, X(5) ) );
}
I am getting a linker error when I have any code that attempts to
insert into this map. As long as I leave the map alone, I am not
getting any linker error.
std::map<>::operator[] requires the mapped_type to be default constructible.
However, I do not think that the other operations do. In any case, you
should get a compiler error, not a linker error. Post code.
Best

Kai-Uwe Bux
Nov 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ryan Liu | last post by:
I have a user control, it must set a property when it is just created, so I set it in its constructor, which has an parameter to specify the property . For example: public class ExecQuest :...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
4
by: Thomas Eichner | last post by:
Hi, does anybody know a public website which offers a service that displays all data send by a browser (or an app calling the website), especially HTTP GET and POST data, browser data etc.? I...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
4
by: Macneed | last post by:
i am a newbie, i remember i read a book talking about when u declare a array variable using float ABC = new float; the whole array element in ABC ( ABC to ABC ) will automatic initialize to 0...
9
by: puzzlecracker | last post by:
"The C# specification states that all value types have a default parameterless constructor, and it uses the same syntax to call both explicitly declared constructors and the parameterless one,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.