944,222 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2919
  • C RSS
Jul 12th, 2005
0

error in copy constructor

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
alone2005 is offline Offline
23 posts
since Jun 2005
Jul 12th, 2005
0

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; }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 12th, 2005
0

Re: error in copy constructor

Thanks a lot, I always misplace the const keyword.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
alone2005 is offline Offline
23 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: #include <GL/glut.h>
Next Thread in C Forum Timeline: Program for gauge monitoring





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC