Assignment on map

A

Andrea Crotti

What's wrong with this thing?

std::map<std::string, Param> int_params;
Param p(1,10);
int_params["sadf"] = p;

Where this is Param?
typedef unsigned int int_param_t;

class Param
{
private:
int_param_t min;
int_param_t max;
int_param_t value;
std::string name;

public:
Param(int_param_t _min, int_param_t _max) : min(_min), max(_max) {}
void set(int_param_t);
void set(const std::string&);
};


Trying to compile it says
expected constructor, destructor, or type conversion before '=' token

But why? the object is created, the map is there...
I also tried with a temporary string but it's the same.

std::map<std::string, Param> int_params;
string s("name");
Param p(1,10);
int_params = p;
 
A

Andrea Crotti

Andrea Crotti said:
What's wrong with this thing?

std::map<std::string, Param> int_params;
Param p(1,10);
int_params["sadf"] = p;

Where this is Param?
typedef unsigned int int_param_t;

class Param
{
private:
int_param_t min;
int_param_t max;
int_param_t value;
std::string name;

public:
Param(int_param_t _min, int_param_t _max) : min(_min), max(_max) {}
void set(int_param_t);
void set(const std::string&);
};


Trying to compile it says
expected constructor, destructor, or type conversion before '=' token

But why? the object is created, the map is there...
I also tried with a temporary string but it's the same.

std::map<std::string, Param> int_params;
string s("name");
Param p(1,10);
int_params = p;


And by the way is there no syntax to do something like:
int_params["name"] = Param(1, 10);

Like this is not correct but I would like to avoid to create n temporary
objects to then add to the map..
 
S

Stuart Golodetz

What's wrong with this thing?

std::map<std::string, Param> int_params;
Param p(1,10);
int_params["sadf"] = p;

This tries to default construct a Param and then assign to it. Param
doesn't have a default constructor, so it fails. You either need to give
Param a default constructor (if it makes sense), or you need to use:

int_params.insert(std::make_pair(std::string("sadf"), p));

Cheers,
Stu
Where this is Param?
typedef unsigned int int_param_t;

class Param
{
private:
int_param_t min;
int_param_t max;
int_param_t value;
std::string name;

public:
Param(int_param_t _min, int_param_t _max) : min(_min), max(_max) {}
void set(int_param_t);
void set(const std::string&);
};


Trying to compile it says
expected constructor, destructor, or type conversion before '=' token

But why? the object is created, the map is there...
I also tried with a temporary string but it's the same.

std::map<std::string, Param> int_params;
string s("name");
Param p(1,10);
int_params = p;
 
A

Andrea Crotti

Stuart Golodetz said:
What's wrong with this thing?

std::map<std::string, Param> int_params;
Param p(1,10);
int_params["sadf"] = p;

This tries to default construct a Param and then assign to it. Param
doesn't have a default constructor, so it fails. You either need to
give Param a default constructor (if it makes sense), or you need to
use:

int_params.insert(std::make_pair(std::string("sadf"), p));

Cheers,
Stu

Uh yes I forgot about the default constructor problem in the map :/
But this doesn't work either, same error...
int_params.insert(std::make_pair(std::string("sadf"), p));
 
S

Stuart Golodetz

Stuart Golodetz said:
What's wrong with this thing?

std::map<std::string, Param> int_params;
Param p(1,10);
int_params["sadf"] = p;

This tries to default construct a Param and then assign to it. Param
doesn't have a default constructor, so it fails. You either need to
give Param a default constructor (if it makes sense), or you need to
use:

int_params.insert(std::make_pair(std::string("sadf"), p));

Cheers,
Stu

Uh yes I forgot about the default constructor problem in the map :/
But this doesn't work either, same error...
int_params.insert(std::make_pair(std::string("sadf"), p));

Hmm, this program compiles and runs for me, does it not for you?

#include <map>
#include <string>

typedef unsigned int int_param_t;

class Param
{
private:
int_param_t min;
int_param_t max;
int_param_t value;
std::string name;

public:
Param(int_param_t _min, int_param_t _max) : min(_min), max(_max) {}
void set(int_param_t);
void set(const std::string&);
};

int main()
{
std::map<std::string, Param> int_params;
Param p(1,10);
//int_params["sadf"] = p;
int_params.insert(std::make_pair(std::string("sadf"), p));
return 0;
}

Regards,
Stu
 
G

Goran

Andrea Crotti said:
What's wrong with this thing?
    std::map<std::string, Param> int_params;
    Param p(1,10);
    int_params["sadf"] = p;
Where this is Param?
typedef unsigned int int_param_t;
class Param
{
private:
    int_param_t min;
    int_param_t max;
    int_param_t value;
    std::string name;
public:
    Param(int_param_t _min, int_param_t _max) : min(_min), max(_max) {}
    void set(int_param_t);
    void set(const std::string&);
};
Trying to compile it says
expected constructor, destructor, or type conversion before '=' token
But why? the object is created, the map is there...
I also tried with a temporary string but it's the same.
    std::map<std::string, Param> int_params;
    string s("name");
    Param p(1,10);
    int_params = p;


And by the way is there no syntax to do something like:
int_params["name"] = Param(1, 10);

Like this is not correct but I would like to avoid to create n temporary
objects to then add to the map..


Can't avoid temps. Think about it: you have one object on the stack,
and you'll have one in the map. There's always one more. The only
thing you can do is to put pointers to your objects in the map, which
is only OK if object copy construction or assignment is more expensive
then going to heap (doesn't seem to be the case here). Note that a
typical map implementation __will__ touch heap to allocate storage for
the node, so it's likely that your copy time is already swamped by
that.

So... Why would you like to avoid temps? If only because you __think__
it's slow, then forget it. You need to care only if you __know__ it's
slow. So do you? At any rate, try to measure your copying time, and
measure total map insertion/cleanup before you jump to this particular
"optimization".

Goran.
 
A

Andrea Crotti

Can't avoid temps. Think about it: you have one object on the stack,
and you'll have one in the map. There's always one more. The only
thing you can do is to put pointers to your objects in the map, which
is only OK if object copy construction or assignment is more expensive
then going to heap (doesn't seem to be the case here). Note that a
typical map implementation __will__ touch heap to allocate storage for
the node, so it's likely that your copy time is already swamped by
that.

So... Why would you like to avoid temps? If only because you __think__
it's slow, then forget it. You need to care only if you __know__ it's
slow. So do you? At any rate, try to measure your copying time, and
measure total map insertion/cleanup before you jump to this particular
"optimization".

Goran.


Eh eh no no nothing like that, the only optimization I wanted is the
minimization of keyboard usage ;)
Since I have to write for many parameters it was easier to write it in
one-line if it was possible, but I can find other solutions...
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top