繁体   English   中英

C++ 复制构造函数中的向量 Arrays

[英]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 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];
    }

}    

编译时我得到的错误是:

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: 注意:候选人是:std::vector< _Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = command, _Alloc = std::allocator]

我 go 如何在复制构造函数中复制向量 arrays?

在你的 class 中,声明了一个成员变量:

command *cmdList;

但是在你的每个构造函数中,你都声明了一个同名的局部变量:

vector <command> cmdList[5];

事实上,您希望成员变量的类型为vector<command>

//  ROBOT CLASS
class Robot {
    private:
…
        std::vector<command> cmdList;    
…
};

然后,在默认构造函数中,您可以为其分配 memory。 您不一定必须这样做,具体取决于您以后如何使用它。

//  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); };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM