Thread: error: passing const std::string as this argument

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    error: passing const std::string as this argument

    The Following is my Code

    FileNotReadableException Signature
    Code:
    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
    Code:
    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 teh Error
    }
    
    const string& FileNotReadableException::getFileName() const{
    	return fileName;
    }
    
    FileNotReadableException::~FileNotReadableException(){
    
    }
    It yields the following Compiler Error.
    Code:
    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.........

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    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.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your member variable shouldn't be const if you are modifying it. It can be a reference, that wasn't causing that problem. But it shouldn't be because you want the string to be owned by the object, not refer to some other string from outside. Making filename a string is the correct choice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  2. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  3. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM