开发者

'No matching function' -error when trying to insert to a set (c++)

开发者 https://www.devze.com 2023-01-05 01:10 出处:网络
I have the following code: class asd { public: int b; asd() { b = rand() % 10; } bool operator<(asd &other) { return b < other.b; }

I have the following code:

class asd {  
    public:  
    int b;  
    asd() { b = rand() % 10; }  
    bool operator<(asd &other) { return b < other.b; }  
};

int main() {  
    asd * c; c = new asd();  
    set <asd> uaua;  
    uaua.insert(c);  
}

Yet when running it, I get this error:

main.cpp|36|error: no matching function for call to ‘std::set<asd, std::less<asd>, std::allocator<asd> >::insert(asd*&)’|

I'm using g++ 4.4.3

Could s开发者_运维百科omeone please tell me where I'm going wrong? I've tried to crack this for a good while, but can't seem to find the solution. Thanks


You have a set of asd, and you're trying to add a pointer.

Use:

asd c; 
set <asd> uaua;
uaua.insert(c);


Try declaring set<asd*> instead of just set<asd>.

0

精彩评论

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

关注公众号