好的,所以 Node 不是您的数组的名称。它是应该包含数组的用户定义类型的名称。但是,您的节点不包含数组。它包含一辆车,名为 a_v。我假设 a_v 应该代表一个车辆数组。因此,您需要分配数组。像这样的东西:
struct Node {
Vehicle a_v[AMOUNT];
};
如果您在编译时不知道您希望数组有多大,则必须动态分配它们,如下所示:
struct Node {
Vehicle* a_v;
Node() {
a_v = new Vehicle[AMOUNT];
}
};
如果它是动态分配的,那么它也必须被释放:
struct Node {
Vehicle* a_v;
Node() {
a_v = new Vehicle[AMOUNT];
}
~Node() {
delete[] a_v;
}
};
如果它是动态分配的,则需要添加用于复制或禁用复制的规定:
struct Node {
Vehicle* a_v;
Node() {
a_v = new Vehicle[AMOUNT];
}
~Node() {
delete[] a_v;
}
// Disable copies (with C++11 support):
Node(const Node&) = delete;
Node& operator=(const Node&) = delete;
// Disable copies (without C++11 support) by making them private and not defining them.
private:
Node(const Node&);
Node& operator=(const Node&);
};
然后要访问其中一辆车,您需要这样做:
Node n; // Declare a node, which contains an array of Vehicles
n.a_v[cont] = v; // Copy a Vehicle into the array of Vehicles
但是请注意,如果您在此函数中声明 Node 实例,则它是本地的,并且一旦您的函数结束,它将超出范围。如果您希望 Node 实例在函数调用之后持续存在,则需要将其声明为 Table 的成员。
class Table
{
private:
Node n;
};
最后,正如其他人所建议的,我强烈建议您阅读 C++ 书籍来学习 C++。我个人推荐的是这本书(第 5 版,不要买第 6 或第 7 版——那些版本的作者很糟糕)。