复制构造函数中的C++矢量数组
C++ Vector Arrays in Copy Constructors
我是复制构造函数的新手,当我开始使用向量时,似乎无法让它们工作。
// ROBOT CLASS
class Robot {
private:
char *name;
int size;
int cmdCount;
command *cmdList;
char items[8][16];
int resources[3]; // silver, gold, then platinum
public:
Robot( );
Robot(int num_of_cmds, const char *nm);
Robot(const Robot &);
~Robot( );
const char *get_name( );
command get_command(int pos) const;
void set_item(const char item[ ], int pos);
const char *get_item(int pos);
};
// ROBOT CONSTRUCTOR default
Robot::Robot( ) {
// Load Robot name
cmdCount = 5;
try {
name = new char[11];
}
catch(std::bad_alloc) {
cout << "Error allocating " << 11+1 << " bytes of memoryn";
name = NULL;
}
if (name) {
strcpy (name, "univac.dat");
}
// Allocate memory for command array
vector <command> cmdList[5];
};
// ROBOT COPY CONSTRUCTOR
Robot::Robot(const Robot &from) {
cmdCount = from.cmdCount;
// Load Robot name
try {
name = new char[11];
}
catch(std::bad_alloc) {
cout << "Error allocating " << 11+1 << " bytes of memoryn";
name = NULL;
}
if (name) {
strcpy (name, from.name);
}
// Allocate memory for command array
vector <command> cmdList[5];
for (int i=0; i < cmdCount;i++) {
cmdList[i] = from.cmdList[i];
}
for (int i=0; i < 8; i++) {
strcpy(items[i], from.items[i]);
}
for (int i=0; i < 3; i++) {
resources[i] = from.resources[i];
}
}
我在编译时得到的错误是:
robot.cpp:在复制构造函数"robot::robot(const robot&)"中:robot.cpp:117:错误:"cmdList[i]=(((command)from->robot::cmdList)+((unsigned int)(((unssigned int)i)*172u))"中的"operator="不匹配/usr/include/c++/4.4/bits/vvector.tcc:156:注意:候选为:std::vector<_Tp,_Alloc>&std::vector<_Tp,_Alloc>::运算符=(const std::vector<_Tp,_Alloc>&)[带_Tp=命令,_Alloc=std::分配器]
如何在复制构造函数中复制矢量数组?
在类中,您声明一个成员变量:
command *cmdList;
但在每个构造函数中,您都声明了一个同名的局部变量:
vector <command> cmdList[5];
事实上,您希望成员变量的类型为vector<command>
:
// ROBOT CLASS
class Robot {
private:
…
std::vector<command> cmdList;
…
};
然后,在默认构造函数中,你可以为它分配内存。你不一定必须这样做,这取决于你以后如何使用它。
// ROBOT CONSTRUCTOR default
Robot::Robot( ) :cmdList(5) {
… // no mention of cmdList in the body required
};
最后,在复制构造函数中,复制它:
// ROBOT COPY CONSTRUCTOR
Robot::Robot(const Robot &from) : cmdList(from.cmdList) {
… // no mention of cmdList in the body required
}
可选:作为可选方案,如果您选择不在构造函数中使用初始化列表,则可以执行以下操作:
Robot::Robot() {
…
cmdList = std::vector<command>(5);
// or cmdList.resize(5);
…
}
Robot::Robot(const Robot &from) {
…
cmdList = from.cmdList;
…
}
额外的信用:如果您进行以下更改,那么您可能根本不需要复制构造函数你也不需要析构函数或赋值运算符:
class Robot {
private:
std::string name;
int size;
int cmdCount;
std::vector<command> cmdList;
char items[8][16];
int resources[3]; // silver, gold, then platinum
public:
Robot( );
Robot(int num_of_cmds, const char *nm);
const char *get_name( ) { return name.c_str(); }
command get_command(int pos) const { return cmdList[pos]; }
void set_item(const char item[ ], int pos);
const char *get_item(int pos);
};
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '