Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7742563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:18:27+00:00 2026-06-01T09:18:27+00:00

I am new to copy constructors and can’t seem to get them working when

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:18:28+00:00Added an answer on June 1, 2026 at 9:18 am

    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);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We get a new copy of data in a pipe-delimited text file from time
Can classes have multiple constructors and/or copy constructors in common-lisp? That is - in
I'm using a C++ base class and subclasses (let's call them A and B
I am trying to return a new copy of the data in a C++
When we copy some text separated by new line and try to paste it
I want to copy a binary master file in a new binary file. This
Brand new to Cocoa and I'm trying to figure out how to copy an
How do I copy files contained within a PathFinder to a new directory?
We just switched to our new website redesign. We have a copy of the
Is it possible to copy a table (with definition, constraints, identity) to a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.