我不知道如何在構造函數中初始化struct向量。任何人都可以給我指針? ^^在構造函數中初始化struct的向量
這是我的結構:
struct Point {
int x;
int y;
};
這是我的頭文件:
class ShapeTwoD {
private:
string name;
bool containsWarpSpace;
vector<Point> vertices;
public:
ShapeTwoD();
ShapeTwoD(string,bool,vector<Point>);
virtual string getName();
virtual bool getContainsWarpSpace();
virtual string toString();
vector<Point> points;
virtual double computeArea() = 0;
virtual bool isPointInShape(int,int) = 0;
virtual bool isPointonShape(int,int) = 0;
virtual void setName(string);
virtual void setContainsWarpSpace(bool);
};
這是我的.cpp文件:
ShapeTwoD::ShapeTwoD() {
name = "";
containsWarpSpace = true;
vertices = "";
}
ShapeTwoD::ShapeTwoD(string name, bool containsWarpSpace,vector<Point>vertices) {
this->name = name;
this->containsWarpSpace = containsWarpSpace;
this->vertices = vertices;
}
它給了我這個錯誤:
ShapeTwoD.cpp:12: error: no match for ‘operator=’ in ‘((ShapeTwoD*)this)->ShapeTwoD::vertices = ""’ /usr/include/c++/4.4/bits/vector.tcc:156: note: candidates are: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = Point, _Alloc = std::allocator]
請閱讀教科書。謝謝。 – Abyx
請測試您自己的代碼,並詢問是否發現實際問題 - 最後一個.cpp文件中的註釋表明您還沒有嘗試過運行它。 – UnholySheep
您在構造函數中初始化事物的方式已中斷。閱讀你的書,它會解釋爲什麼。 – Griwes