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

is it possible to make a map of objects?

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::stringListOfWorkingSchedule;
Aug 16 '07 #1
10 1582
Milan Krejci wrote:
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::stringListOfWorkingSchedule;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '07 #2

"Milan Krejci" <rd*@no-spam.mail.czwrote in message
news:fa***********@news.vol.cz...
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::stringListOfWorkingSchedule;
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::stringListOfWorkingSchedule;

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

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
Aug 16 '07 #3
Paul napsal(a):
"Milan Krejci" <rd*@no-spam.mail.czwrote in message
news:fa***********@news.vol.cz...
>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::stringListOfWorkingSchedule;

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::stringListOfWorkingSchedule;

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

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
Aug 16 '07 #4
Milan Krejci wrote:
Paul napsal(a):
>"Milan Krejci" <rd*@no-spam.mail.czwrote in message
news:fa***********@news.vol.cz...
>>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::stringListOfWorkingSchedule;

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::stringListOfWorkingSchedule;

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

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) {
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '07 #5
On 2007-08-16 15:00, Milan Krejci wrote:
Paul napsal(a):
>"Milan Krejci" <rd*@no-spam.mail.czwrote in message
news:fa***********@news.vol.cz...
>>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::stringListOfWorkingSchedule;

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::stringListOfWorkingSchedule;

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

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)
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.
--
Erik Wikström
Aug 16 '07 #6
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):
Milan Krejci wrote:
>Paul napsal(a):
>>"Milan Krejci" <rd*@no-spam.mail.czwrote in message
news:fa***********@news.vol.cz...
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::stringListOfWorkingSchedule;
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::stringListOfWorkingSchedule;

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

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) {

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
Aug 17 '07 #7
Hi!

Milan Krejci schrieb:
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
Aug 17 '07 #8
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):
Hi!

Milan Krejci schrieb:
>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
Aug 17 '07 #9
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):
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):
>Hi!

Milan Krejci schrieb:
>>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
Aug 17 '07 #10
Hi!

Milan Krejci schrieb:
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
Aug 17 '07 #11

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

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
3
by: ZhangZQ | last post by:
Supposing I have a class "UserControl1 : System.Windows.Forms.UserControl" in an Assembly "UI.dll", now I create an Application project and want to use the "UserControl1" in another AppDomain, ...
2
by: Bhupesh Naik | last post by:
This is a query regarding my problem to make a spell and grammar check possible in text area of a web page. We have aspx pages which are used to construct letters. The browser based screens...
13
by: ThunderMusic | last post by:
Hi, I have to develop an application that will consist of a service and a windows forms application... the service could be on a distant server but I need to authenticate the user as being part...
2
by: Daz | last post by:
Hi everyone. Sorry for the confusing subject, I couldn't think how best to word it. What I would like to know, is if there is an equivilant to this code, in using JSON. <script...
11
by: Andy Watson | last post by:
I have an application that scans and processes a bunch of text files. The content I'm pulling out and holding in memory is at least 200MB. I'd love to be able to tell the CPython virtual machine...
13
by: ragged_hippy | last post by:
Hi, I wanted to create a vector of const references. Something like this: vector<const x&y; where x is a class name. Is this a valid statement? Will this work if I use a reference of 'y'...
2
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I want to give a user the possibility of "restarting" an interactive session, by removing all the objects defined by her since the beginning. The way I make this possible is by having a "function"...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.