我已經定義了類的「結果」和「Bin」。 我想將類型數組的數組傳遞給Bin構造函數,以便將該數組的每個元素添加到一組「結果」,它是Bin類的成員屬性。如何將數組的元素添加到集合
//Bin.h
class Bin {
private:
std::set<Outcome> outcomeset;
public:
Bin();
Bin(Outcome Outcs[], int numberofelements);
Bin(std::set<Outcome> Outcs);
void add(Outcome Outc);
std::string read();
};
//In Bin.cpp
Bin::Bin(Outcome outcs[], int numberofelements) {
int i;
for (i=0;i<(numberofelements-1);i++) {
outcomeset.insert(outcs[i]); //When this LIne is commented out, no compile errors!
}
}
這會導致VS2010中的錯誤鏈接回庫文件。我一直無法在網上或在「The Big C++」教科書中找到任何東西。這是這種功能的完全錯誤的實現嗎?或者我錯過了一些相當基本的東西?
對於好奇的我從這個免費教科書實施這一爲「輪盤賭」問題的一部分http://www.itmaybeahack.com/homepage/books/oodesign.html
感謝您的幫助!
編輯:我已經添加了(相當長的)錯誤文本到引擎收錄,在這裏:http://pastebin.com/cqe0KF3K
EDIT2:我已經實現了== =和<運營商的結果類,並在同一行依舊!不編譯。這裏是實現
//Outcome.cpp
bool Outcome::operator==(Outcome compoutc) {
if (mEqual(compoutc) == true) {
return true;
}
else {
return false;
}
}
bool Outcome::operator!=(Outcome compoutc) {
if (mEqual(compoutc) == false) {
return true;
}
else {
return false;
}
}
bool Outcome::operator<(Outcome compoutc) {
if (odds < compoutc.odds) {
return true;
}
else {
return false;
}
}
EDIT3:實現比較運算符與去引用參數和const標記,現在它編譯!
你的類'Outcome'不提供比較運算符。 –
'我<(numberofelements-1)'看起來像一個錯誤的錯誤。如果列表中有3個元素,我將[0,1],你會錯過索引2. – Thanatos
我現在已經爲Outcome類實現了==!=運算符,上面的Bin構造函數仍然不能編譯。 (謝謝你的回覆) @Thanatos,漂亮的眼睛,我刪除了它,但它對編譯錯誤沒有任何影響,我會發布一些錯誤信息到原始文章。 ! –