RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 1656 | Replies: 2 | Solved
Reply
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

error in copy constructor

  #1  
Jul 12th, 2005
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,763
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: error in copy constructor

  #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; }
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: error in copy constructor

  #3  
Jul 12th, 2005
Thanks a lot, I always misplace the const keyword.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:46 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC