is it possible to make a map of objects?

M

Milan Krejci

hello, sorry to bother you with this but i can't seem to figure out why
i can't do:

struct SD {
int from;
int to;
} sd;
std::map<sd,std::string> ListOfWorkingSchedule;
 
V

Victor Bazarov

Milan said:
hello, sorry to bother you with this but i can't seem to figure out
why i can't do:

struct SD {
int from;
int to;
} sd;

Drop the 'sd' here. You're declaring an object for no reason. Your
struct is called 'SD' (as you wrote yourself).
std::map<sd,std::string> ListOfWorkingSchedule;

If you need a map where the type 'SD' is the key, you need to say so:

std::map<SD,std::string> ..

You can't specify an _object_ where a _type_ is expected.

Also, consider that your type ('SD') needs the sorting capabilities
to be used as the key in the standard 'map' container. You will need
to implement

bool operator <(SD const& sd1, SD const& sd2)
{
...
}

somewhere next to 'SD' type definition.

V
 
P

Paul

Milan Krejci said:
hello, sorry to bother you with this but i can't seem to figure out why i
can't do:

struct SD {
int from;
int to;
} sd;
std::map<sd,std::string> ListOfWorkingSchedule;

First, "sd" is the name of the variable, not the type:
--------------------------------------------------------------------
#include <map>
#include <string>

struct SD {
int from;
int to;
}sd;

std::map<SD, std::string> ListOfWorkingSchedule;

--------------------------------------------------------------------

After this change, you will still get a compiler error, albeit a different
one. Then once you get this new error, try to understand what it means. If
you can't figure it out, post the compiler error (I know what the problem
is -- I'm seeing if you know what the problem is).

Paul
 
M

Milan Krejci

Paul napsal(a):
First, "sd" is the name of the variable, not the type:
--------------------------------------------------------------------
#include <map>
#include <string>

struct SD {
int from;
int to;
}sd;

std::map<SD, std::string> ListOfWorkingSchedule;

--------------------------------------------------------------------

After this change, you will still get a compiler error, albeit a different
one. Then once you get this new error, try to understand what it means. If
you can't figure it out, post the compiler error (I know what the problem
is -- I'm seeing if you know what the problem is).

Paul
yes, it complains about missing overloaded < operator. i added one but
don't know if correctly
bool operator<(SD const &sd1) {
if (sd1.from<this.from)
return true;
}

doba.h:11: error: `from' is not a type
doba.h:11: error: request for member of non-aggregate type before ')' token
 
V

Victor Bazarov

Milan said:
Paul napsal(a):
yes, it complains about missing overloaded < operator. i added one but
don't know if correctly
bool operator<(SD const &sd1) {

Since you made it a member, you might want to declare it 'const':

bool operator<(SD const &sd1) const {
if (sd1.from<this.from)

'this' is a pointer. It needs the arrow (->), not the dot (.) to get
to a member. And inside a member function you don't really need to
use 'this' to get to 'from'.
return true;
}

doba.h:11: error: `from' is not a type
doba.h:11: error: request for member of non-aggregate type before ')'
token

V
 
?

=?ISO-8859-2?Q?Erik_Wikstr=F6m?=

Paul napsal(a):
yes, it complains about missing overloaded < operator. i added one but
don't know if correctly
bool operator<(SD const &sd1) {
if (sd1.from<this.from)

this is a pointer, so you need to use ->

if (sd1.from < this->from)

or

if (sd1.from < from)

By the way, this way you have implemented it so that if sd1.from is
smaller than this->from, sd1 is considered to be larger than this.
 
M

Milan Krejci

Thanks! now it works only another issue has raised. when i try to list
my map it is executed way too many times for no apparent reason.

sd=new SD();
sd->from=first;
sd->to=last;
SDoby.insert(std::make_pair(*sd,typ));

std::map<SD,std::string>::iterator it;
int a,b; SD s;
for(it = SDoby.begin(); it != SDoby.end(); it++)
{ s=it->first;
a=s.vrat_from();
b=s.vrat_to();
std::cout << a << " " << it->second << std::endl; //executed way more
times than is supposed to be
}

Victor Bazarov napsal(a):
 
F

Frank Birbacher

Hi!

Milan said:
Thanks! now it works only another issue has raised. when i try to list
my map it is executed way too many times for no apparent reason.

sd=new SD();
sd->from=first;
sd->to=last;
SDoby.insert(std::make_pair(*sd,typ));

Try to avoid "new":

SD sd;
sd.from = first;
sd.to = last;
SDoby.insert(std::make_pair(sd,typ));
std::cout << a << " " << it->second << std::endl; //executed way
more times than is supposed to be

How many times did it execute? How many times should it execute? It's
best to provide the output your program gives AND to provide what you
expected.

Frank
 
M

Milan Krejci

Hello,
firstly-
std::vector <LCDRange *>::iterator it;
LCDRange *l;
for (it=vec->begin();it!=vec->end();it++) { // is executed N times
l=*(it);
l->p_doba->list();
l->p_doba->checkSanity(l->day->day,t);
l->p_doba->show_ListOfWorkingSchedule(t);
}
in checkSanity i call
this->add(1,10,"i am desperate",t);
which does
SD sd;
sd.from=first;
sd.to=last;
ListOfWorkingSchedule.insert(std::make_pair(sd,typ)); //typ=="i am
desperate"
so far so good
but show_ListOfWorkingSchedule(t) contains
std::map<SD,std::string>::iterator itd;
int a,b; SD s;
for(itd = ListOfWorkingSchedule.begin(); itd !=
ListOfWorkingSchedule.end(); itd++)
{ s=itd->first;
a=s.return_from();
b=s.return_to();
std::cout << a << " " << itd->second << std::endl; //is
executed N times instead of how many items have been added
}

Frank Birbacher napsal(a):
 
R

Richard Herring

Milan Krejci said:
Paul napsal(a):
yes, it complains about missing overloaded < operator. i added one but
don't know if correctly
bool operator<(SD const &sd1) {
if (sd1.from<this.from)
return true;
}

doba.h:11: error: `from' is not a type
doba.h:11: error: request for member of non-aggregate type before ')' token

Once you've made the obvious correction to fix these errors, consider
this:

If the condition is false, what does the operator return?

If your operator< doesn't define a strict weak ordering (look it up) the
map will not behave as you would expect.
 
M

Milan Krejci

it seems the trouble was with the overloaded <
when i wrote
if (from<sd1.from)
return true; else return false;
}
it started to work as magic. thanks everybody.


Milan Krejci napsal(a):
 
M

Milan Krejci

Richard Herring napsal(a):
Once you've made the obvious correction to fix these errors, consider this:

If the condition is false, what does the operator return?

If your operator< doesn't define a strict weak ordering (look it up) the
map will not behave as you would expect.
thanks for help, mate.
 
F

Frank Birbacher

Hi!

Milan said:
it seems the trouble was with the overloaded <
when i wrote
if (from<sd1.from)
return true; else return false;
}
it started to work as magic. thanks everybody.

This is the same as
return from < sd1.from;


Maybe you can delete old parts of your messages when replying here, please.

Frank
 

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
473,904
Messages
2,570,003
Members
46,359
Latest member
thejackson123

Latest Threads

Top