我正在嘗試創建一個新的 Node 類并在我的名為 Colony 的類中設定它的坐標(我的運行函式在 Colony 類中)。雖然它是段錯誤。我嘗試過使用 new 但它不起作用。這里應該解決什么問題?下面是代碼片段:
class Node {
public:
std::vector<double> coords;
vector<Node> parent;
vector<Node> childList;
vector<Attract> closestAtts;
};
class Colony {
public:
double D, dk, di;
vector<Attract> attlist;
vector<Node> nodelist;
std::vector<vector<double> > attractors;
std::vector<vector<double> > centroids;
void run() {
// Initializes a colony with starting point
Node start;
start.coords[0] = 100.0;
start.coords[1] = 100.0;
start.coords[2] = 100.0;
nodelist.push_back(start);
}
uj5u.com熱心網友回復:
在您的run()方法中,您嘗試分配start.coords向量的第 0、第 1 和第 2 個元素,但尚未分配這些元素。相反,您應該使用.push_back這些值,如下所示:
void run() {
// Initializes a colony with starting point
Node start;
start.coords.push_back(100.0);
start.coords.push_back(100.0);
start.coords.push_back(100.0);
nodelist.push_back(start);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/488788.html
