Don Lun
Don Lun

Reputation: 2777

C++ question. about container and instance of class

A* a = new A(x,y);
set<A> aset;
aSet.insert(a);

I did this. Got an error. How should I fix it?

Thank you!!!

Upvotes: 0

Views: 100

Answers (2)

Michael Madsen
Michael Madsen

Reputation: 54989

You're trying to insert a pointer to an A into your set, but the set is declared as taking As directly.

You must either change your set to store pointers:

A* a = new A(x,y);
set<A*> aset;
aSet.insert(a); 

or create an instance, rather than a pointer to an instance:

A a = A(x,y);
set<A> aset;
aSet.insert(a); 

Upvotes: 3

Marius Bancila
Marius Bancila

Reputation: 16318

aset is a set of A, not of pointers to A. So either

set<A*> aset;

or

aset.insert(*a);

but don't think the later makes too much sense.

Upvotes: 7

Related Questions