c++将项存储到数组中
c++ store items into an array
我有这个代码,在我的脑海中,它收到一个名为Vehicle的项目,它必须将其存储在一个名为Node的数组中。以下是与这部分程序相关的代码:
void Table::process(Vehicle v, int cont) {
char a='A'+cont;
putVehicle(a,v);
Node.a_v[cont]=v;
if(cont==0) a_surt=v.rowVehicle();
}
这就是我如何在Table.h的私有部分设置数组:
struct Node{
Vehicle a_v;
};
我得到的错误是:
error: expected primary-expression before '.' token
我有我需要的包括,但每次我输入这个:Node.a_v
它给我那个错误。任何建议吗?
如果你想使用一个结构体,你需要在使用它之前声明一个Node
。此外,该结构体需要包含数组(或者更好的是,查看vector 以获得更大的灵活性)。
struct Node {
Vehicle[10] a_v; // 10 is max number of Vehicles in array
};
Node myNode;
myNode.a_v[cont] = v;
请记住,如果您想保留这个Node
并在其中放置更多的东西,则需要在正确的作用域中声明它。例如,要让您的process
函数将Vehicle
添加到存在于函数process
之外的Node
中,您可以这样做:
void Table::process(Node n, Vehicle v, int cont) {
char a = 'A'+cont;
putVehicle(a,v);
if (cont < 10) {
n.a_v[cont] = v;
}
if (cont == 0) a_surt = v.rowVehicle();
}
看起来你只是想使用一个数组。在这种情况下,你要找的是这样的内容:
// This would go somewhere in your program. Again, 10 is just an example.
Vehicle vehicleArray[10];
// Send this array to this function
void Table::process(Vehicle[] vArray, Vehicle v, int cont) {
char a = 'A'+cont;
putVehicle(a,v);
if (cont < 10) { // In a real program, don't hard-code array limits.
vArray[cont] = v;
}
if (cont == 0) a_surt = v.rowVehicle();
}
您应该使用Node
对象来访问a_v
变量。这条线
Node.a_v[cont]=v;
是不正确的。你应该这样做:
Node n;
n.a_v[cont]=v;
每次我输入这个:Node。它给了我那个错误。
Node
是一个类型;类型定义了对象的结构,但是它们没有自己的字段(除了static
字段,它一次属于所有实例;它们的访问方式不同)。
为了使用.
或->
操作符,您需要Node
的实例,如下所示:
Node x;
x.a_v = ...
这是不清楚在您的情况下,从哪里Node
实例应该来,虽然。为了访问它们,您需要将它们作为参数传递,或者使它们静态/全局可用(不推荐)。
好的,所以Node不是数组的名称。它是用户定义的类型的名称,应该包含一个数组。但是,您的Node不包含数组。它包含一个名为a_v的Vehicle。我假设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版——那些版本的作者很糟糕)。
- 多维数组存储三种不同的数据类型?
- C++将原始字符数组存储在元组中
- 将十六进制字符数组存储在字节数组中,而无需更改为 ASCII 或其他任何东西
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- 如何将元素的索引从2D数组存储到1D数组中,然后交换这些值
- 如何将JSON数组存储在Arduino程序中
- 如何在AVR(例如:ATMega)或Arduino微控制器上制作2D PROGMEM阵列的1D PROGMEM数组(存储
- 使用 char 数组存储数据
- 使用平面数组存储的元组容器
- 数组 存储并要求用户搜索它
- 将值从一个 char 数组存储到另一个 char 数组
- 如何使字符串数组存储在向量中
- 将构造的静态数组存储在多个文件中的内存中
- 将整数数组存储在堆上并通过指针访问它们
- c++声明一个数据类型的字节数组,该数组存储在字符串中
- 使用指针数组存储阶乘计算的所有中间结果
- 使用异构数组存储不同的子类
- 读取文件内容,存储在数组中,向数组添加更多内容,然后将新数组存储在文件C++中
- 尝试将复杂数组存储到类 c++ 中
- 在字节数组存储中嵌入 int/string