2013-06-12 100 views
0

下面的類是本:插入對象插入集失敗

class Actor { 
public: 

    float xpos{0}; 
    float ypos{0}; 

    Actor(float x, float y); 
    ~Actor(); 
}; 

在管理類的靜態函數,我想創建這樣的演員,將其插入一組:

class ActorManager { 
private: 
    ActorManager(); 
    static std::set<Actor> actors; 
public: 
    static void addActor(float x, float y); 
} 

定義:

std::set<Actor> ActorManager::actors = std::set<Actor>(); 

void ActorManager::addActor(float x, float y) { 
    Actor actor(x, y); 
    actors.insert(actor); // <-- 
} 

隨着標線目前actors.insert,編譯失敗。錯誤狀態:

/usr/lib/c++/v1/__functional_base:56:21: Invalid operands to binary expression ('const Actor' and 'const Actor') 

我在這裏錯過了什麼?

+0

你不會錯過任何的正確途徑。你在'actions = std :: Set ()'的末尾有一個無關的'()'。 – kfsone

+0

似乎需要初始化靜態成員,如果我使用'{}',則會出現錯誤或者將它們排除。 – Appleshell

回答

3

你需要重載operator<才能使用你的課std::set(它需要這個爲了排序元素)。

+0

現在錯誤信息也是有意義的。謝謝! – Appleshell

0
bool operator <(const Actor& p1, const Actor& p2){ 
bool result=false; 
if (p1.x<p2.x) 
{ 
result=true; 
} 
else if (p1.x==p2.x&&p1.y<p2.y){ 
result=true; 
} 
return result; 

}

//這是重載<操作