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.

Recommended Answers

All 2 Replies

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; }

Thanks a lot, I always misplace the const keyword.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.