copy constructor causing errors

This is a discussion on copy constructor causing errors within the C++ Programming forums, part of the General Programming Boards category; i have a class that looks like this Code: class player { public: player(string name = "", int si = ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    copy constructor causing errors

    i have a class that looks like this

    Code:
    class player
    {
      public:
        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);}
        
      private:
        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:
    Code:
     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 08:39 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Doesn't a copy constructor need to be of the following form?
    Code:
    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
    Posts
    85
    yup he's right

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Not like this.
    Code:
    player(player rhs)
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    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-13-2008, 11:06 PM
  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, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21