Jump to content
  • Advertisement
Sign in to follow this  
nlbs

error: passing const std::string as this argument

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The Following is my Code FileNotReadableException Signature
class FileNotReadableException : public CGIException{
	protected:
		const string& fileName;
	public:
		FileNotReadableException();
		FileNotReadableException(const string& file);
		~FileNotReadableException();
		void setFileName(const string& file);
		const string& getFileName() const;
};

FileNotReadableException Implementation
FileNotReadableException::FileNotReadableException():fileName(""), CGIException(){

}

FileNotReadableException::FileNotReadableException(const string& file):fileName(file), CGIException("CGI::System::FileUnReadable", ("File: "+fileName+" Couldn't be opened for reading")){

}

void FileNotReadableException::setFileName(const string& file){
	fileName = file;//Line 43 Here is the Error
}

const string& FileNotReadableException::getFileName() const{
	return fileName;
}

FileNotReadableException::~FileNotReadableException(){

}

It yields the following Compiler Error.
Line 43: error: passing ‘const std::string’ as ‘this’ argument of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::bas.........

Share this post


Link to post
Share on other sites
Advertisement
In general, don't use references as members unless you are very sure of what you are doing. You could instead make "fileName" a std::string instance.

Also, I do not understand the need for a setter for the filename member - why would you ever change the file name of such an exception? Also: is there a need for the default constructor? If a file can be detected as not being readable then it probably has a name.

Share this post


Link to post
Share on other sites
Thanks solved by using string instead of const string&
I can understand why the const was making the problem (const-correctness)
But cant understand why the reference was making a Problem cause I am initializing all of them on teh Constructor.

Share this post


Link to post
Share on other sites
When you create a FileNotFoundException instance, you are probably not passing it a string that will survive the stack unwinding this will cause (this is a guess - perhaps you are). You should only use a reference as a member (function arguments are fine) when you can be 100% sure that the referenced variable will have the same (or greater) lifetime as the reference holder.

Share this post


Link to post
Share on other sites
Quote:
Original post by nlbs
Thanks solved by using string instead of const string&
I can understand why the const was making the problem (const-correctness)
But cant understand why the reference was making a Problem cause I am initializing all of them on teh Constructor.
Because when you assign something to a reference, you're assigning something to the value of the string. You have a const reference as a member variable, so you can't assign to it (It's const).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!