复制构造函数中的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默认
Robot :: Robot(){
//加载机器人名称
cmdCount = 5;
try {
name = new char [11];
}
catch(std :: bad_alloc){
cout< 错误分配< 11 + 1<< bytes of memory\\\
;
name = NULL;
}
if(name){
strcpy(name,univac.dat);
}
//为命令数组分配内存
vector< command> cmdList [5];
};
// ROBOT COPY CONSTRUCTOR
Robot :: Robot(const Robot& from){
cmdCount = from.cmdCount;
//加载机器人名称
try {
name = new char [11];
}
catch(std :: bad_alloc){
cout< 错误分配< 11 + 1<< bytes of memory\\\
;
name = NULL;
}
if(name){
strcpy(name,from.name);
}
//为命令数组分配内存
vector< command> cmdList [5];
for(int i = 0; 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)((unsigned int) i)* 172u)))'
/usr/include/c++/4.4/bits/vector.tcc:156:note:candidate:std :: vector <_Tp,_Alloc>& std :: vector <_Tp,_Alloc> :: operator =(const std :: vector <_Tp,_Alloc>&)[with _Tp = command,_Alloc = std :: allocator]
< blockquote>
如何复制复制构造函数中的向量数组?
解决方案在你的类中,你声明一个成员变量:
command * cmdList;但是在每个构造函数中,你声明一个具有相同名称的局部变量:
vector< command> cmdList [5];
事实上,你希望成员变量的类型是
vector< command>
:// ROBOT CLASS
class Robot {
private:
...
std :: vector< command> cmdList;
...
};
然后,在默认构造函数中,你可以为它分配内存。
// ROBOT CONSTRUCTOR默认
机器人:: Robot():cmdList(5){
... //没有提到cmdList在正文中需要
};
最后,在复制构造函数中,复制它:
// ROBOT COPY CONSTRUCTOR
Robot :: Robot(const Robot& from):cmdList(from.cmdList){
... // no mention在需要的主体中的cmdList
}
替代:如果您选择不在构造函数中使用初始化列表,您可以这样做:Robot :: Robot(){
...
cmdList = std :: vector< command>(5);
//或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(); }
命令get_command(int pos)const {return cmdList [pos]; }
void set_item(const char item [],int pos);
const char * get_item(int pos);
};
I am new to copy constructors and can't seem to get them working when I start using vectors.
// 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 memory\n"; 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 memory\n"; 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]; } }
The error I get when compiling is:
robot.cpp: In copy constructor 'Robot::Robot(const Robot&)': robot.cpp:117: error: no match for 'operator=' in 'cmdList[i] = (((command)from->Robot::cmdList) + ((unsigned int)(((unsigned int)i) * 172u)))' /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 = command, _Alloc = std::allocator]
How would I go about copying vector arrays in a copy constructor?
解决方案In your class, you declare a member variable:
command *cmdList;
But in each of your constructors, you declare a local variable with the same name:
vector <command> cmdList[5];
In fact, you'll want the type of the member variable to be
vector<command>
:// ROBOT CLASS class Robot { private: … std::vector<command> cmdList; … };
Then, in the default constructor, you may allocate memory for it. You don't necessarily have to, depending upon how you later use it.
// ROBOT CONSTRUCTOR default Robot::Robot( ) :cmdList(5) { … // no mention of cmdList in the body required };
Finally, in the copy constructor, copy it:
// ROBOT COPY CONSTRUCTOR Robot::Robot(const Robot &from) : cmdList(from.cmdList) { … // no mention of cmdList in the body required }
Alternative: As an alternative, if you choose not to use initialization lists in your constructors, you can do this:Robot::Robot() { … cmdList = std::vector<command>(5); // or cmdList.resize(5); … } Robot::Robot(const Robot &from) { … cmdList = from.cmdList; … }
Extra Credit: If you make the following changes, then you may not need copy constructors at all! Neither will you need destructors or assignment operators: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); };
这篇关于复制构造函数中的C ++向量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!