error in copy constructor

Thread Solved
Reply

Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

error in copy constructor

 
0
  #1
Jul 12th, 2005
Potion of my code:
  1. class UserCommand{
  2. string cHandle; //definition of command
  3. int numPara;
  4. vector<int> cPara; //parameter of command
  5. public:
  6. UserCommand(string ucData);
  7. //copy constructor
  8. UserCommand(const UserCommand& uc);
  9.  
  10. inline const string getHandle(){return cHandle;};
  11. inline const int getPara(int i){return cPara.at(i);};
  12. inline const int getNumPara(){return numPara;};
  13.  
  14. //overload operator = here
  15. inline UserCommand operator=(UserCommand& uc);
  16. }; //
  17.  
  18. //...
  19.  
  20. UserCommand::UserCommand(const UserCommand& uc){
  21. cHandle=uc.getHandle();
  22. numPara=uc.getNumPara();
  23. for(int i=0;i<numPara;i++) cPara[i]=uc.getPara(i);
  24. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,303
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: error in copy constructor

 
0
  #2
Jul 12th, 2005
	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; }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: error in copy constructor

 
0
  #3
Jul 12th, 2005
Thanks a lot, I always misplace the const keyword.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC