开发者

C++ question. about container and instance of class

开发者 https://www.devze.com 2023-03-05 18:32 出处:网络
A* a = new A(x,y); set<A> aset; aSet.insert(a); I did this. Got an error. How shou开发者_开发百科ld I fix it?
A* a = new A(x,y);
set<A> aset;
aSet.insert(a);

I did this. Got an error. How shou开发者_开发百科ld I fix it?

Thank you!!!


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.


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); 
0

精彩评论

暂无评论...
验证码 换一张
取 消