DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   error in copy constructor (http://www.daniweb.com/forums/thread27996.html)

alone2005 Jul 12th, 2005 2:54 pm
error in copy constructor
 
Potion of my code:
class UserCommand{
        string cHandle;                        //definition of command
        int numPara;
        vector<int> cPara;                //parameter of command
public:
        UserCommand(string ucData);
        //copy constructor
        UserCommand(const UserCommand& uc);
       
        inline const string getHandle(){return cHandle;};
        inline const int getPara(int i){return cPara.at(i);};
        inline const int getNumPara(){return numPara;};
       
        //overload operator = here
          inline UserCommand operator=(UserCommand& uc);
}; //

//...

UserCommand::UserCommand(const UserCommand& uc){
        cHandle=uc.getHandle();
        numPara=uc.getNumPara();
        for(int i=0;i<numPara;i++) cPara[i]=uc.getPara(i);
}

Pretty straighforward. But I keep got error:
error: passing `const UserCommand' as `this' argument of `std::string UserCommand::getHandle()' discards qualifiers

I am using gcc under redhat, anyone can help me on this? thanks in advance.

Dave Sinkula Jul 12th, 2005 3:18 pm
Re: error in copy constructor
 
        inline const string getHandle(){return cHandle;};
        inline const int getPara(int i){return cPara.at(i);};
        inline const int getNumPara(){return numPara;};
I believe it's telling you it would prefer that the this pointer be const. (And function definitions don't end with a semicolon.)
   inline string getHandle() const { return cHandle; }
  inline int getPara(int i) const { return cPara.at(i); }
  inline int getNumPara()  const { return numPara; }

alone2005 Jul 12th, 2005 7:20 pm
Re: error in copy constructor
 
Thanks a lot, I always misplace the const keyword.


All times are GMT -4. The time now is 11:16 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC