Thread: copy constructor causing errors

  1. #1
    Registered User
    Join Date
    Nov 2005

    copy constructor causing errors

    i have a class that looks like this

    class player
        player(string name = "", int si = 0):_name(name){_spriteIndex = si; instanceCount++; cout << endl << "constructor of " << this->_name << ":: there are " << instanceCount << " players" << endl;}
        ~player(){instanceCount--; cout << endl << "destructor of " << this->_name << ":: there are " << instanceCount << " players" << endl;}
        //player(player &rhs){_name = rhs.get_name(); _spriteIndex = rhs.get_spriteIndex(); instanceCount++; cout << "copy constructor:: there are " << instanceCount << " players" << endl;}
        string get_name() const {return (_name);}
        void set_name(string name){_name = name;}
        int get_spriteIndex() const {return (_spriteIndex);}
        void set_spriteIndex(int si){_spriteIndex = si;}
        static int get_instanceCount() {return (instanceCount);}
        string _name;    
        int _spriteIndex;
        static int instanceCount;
    when i leave the comment in i can compile the program with no problem. when i take it out, it doesn't increment the instanceCount when it is coppied, which i need it to do. i get an error when i try to initilize a vector of type player.

    it says:
     in concstructor 'std::vector<_Tp,_Alloc>::vector(size_t)[with_Tp = player,_Alloc = std::allocator<player>]'
    line 50 : vector<player> players(0); instantiated from here
    no matching function for call to 'player::player(player)'
    candidates are player::player(plyaer&)
    	player::player(std::string, int)
    in function 'void std::_Construct(_T1*,const_T2&)[with _T1 = player,_T2 = player]'
    why is the copy constructor causing errors and how can i fix it. thank you for your help.
    Last edited by yahn; 12-22-2005 at 09:39 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Doesn't a copy constructor need to be of the following form?
    player(const player &rhs)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    yup he's right

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Not like this.
    player(player rhs)

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2008, 12:06 AM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 12:28 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM