954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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.

alone2005
Newbie Poster
23 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 
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 thethis 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; }
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Thanks a lot, I always misplace the const keyword.

alone2005
Newbie Poster
23 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You