std::map problem

O

O.B.

In the test program below, the compiler complains that it cannot deduce the
argument when assigning an int value to an entry in a std::map. What am I doing
wrong?


#include <map>

class EntityID {

public:
unsigned int site;
unsigned int application;
unsigned int entity;
};

void main() {

std::map<EntityID,int> test;

EntityID id1;

id1.site = 6;
id1.application = 5;
id1.entity = 4;

test[id1] = 5;

EntityID id2;

id2.site = 6;
id2.application = 5;
id2.entity = 4;

std::map<EntityID,int>::const_iterator i = test.find(id2);
if ( i != test.end() ) {
printf("Test passed.\n");
} else {
printf("Test failed.\n");
}

}
 
V

Victor Bazarov

O.B. said:
In the test program below, the compiler complains that it cannot deduce
the argument when assigning an int value to an entry in a std::map.
What am I doing wrong?

Just a couple of things.
#include <map>

class EntityID {

public:
unsigned int site;
unsigned int application;
unsigned int entity;
};

void main() {

int main() {
std::map<EntityID,int> test;

Your 'EntityID' class doesn't have 'operator<' defined for it, and thus
it cannot be used as the key for a map. You need to define how one
EntityID object is "less" than another. RTFM.
EntityID id1;

id1.site = 6;
id1.application = 5;
id1.entity = 4;

test[id1] = 5;

EntityID id2;

id2.site = 6;
id2.application = 5;
id2.entity = 4;

std::map<EntityID,int>::const_iterator i = test.find(id2);
if ( i != test.end() ) {
printf("Test passed.\n");
} else {
printf("Test failed.\n");
}

}

V
 
J

Jeff Flinn

O.B. said:
In the test program below, the compiler complains that it cannot deduce the
argument when assigning an int value to an entry in a std::map. What am I doing
wrong?


#include <map>

class EntityID {

public:
unsigned int site;
unsigned int application;
unsigned int entity;
};

Modify EntityID such that the following compiles, and your code should
compile and run.

int main()
{
EntityID id1;

id1.site = 6;
id1.application = 5;
id1.entity = 4;

EntityID id2;

id2.site = 6;
id2.application = 5;
id2.entity = 4;

bool Id1LessThanId2 = id1 < id2;
bool Id2LessThanId1 = id2 < id1;

return 0;
}

Jeff F
void main() {

std::map<EntityID,int> test;

EntityID id1;

id1.site = 6;
id1.application = 5;
id1.entity = 4;

test[id1] = 5;

EntityID id2;

id2.site = 6;
id2.application = 5;
id2.entity = 4;

std::map<EntityID,int>::const_iterator i = test.find(id2);
if ( i != test.end() ) {
printf("Test passed.\n");
} else {
printf("Test failed.\n");
}

}
 

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
474,183
Messages
2,570,970
Members
47,526
Latest member
RoslynDavi

Latest Threads

Top