g++ compiler error

I have a function here that compiles perfectly with VC++ 2013. It's function is to return a string whose whitespaces have been removed. I understand there are alternatives to go about this method, but I'm not interested in "isspace" or whatever.

1
2
3
4
5
const std::string & RemoveWhitespace(std::string &inputStr)
{
	inputStr.erase(std::remove(inputStr.begin(), inputStr.end(), ' '), inputStr.end());
	return inputStr;
} 


This does the job on my Windows machine. However, on a UNIX server with g++, I get the following error:

1
2
3
4
 error: in passing argument 1 of ‘const string& RemoveWhitespace(std::string&)’
 extern const std::string & RemoveWhitespace(std::string &inputStr);
                            ^
file.cpp:87:123: error: invalid initialization of non-const reference of type ‘std::string& {aka std::basic_string<char>&}’ from an rvalue of type ‘std::basic_string<char>’


Anyone know of a solution for this problem?
Last edited on
Are you passing a temporary object to RemoveWhitespace by any chance? Temporary objects can't bind to non-const references.
Last edited on
you receive as parameter a non-const reference and then you send it as const , that might be your error.
yeah, I was accessing a string from a custom class vector. I fixed the problem by assigning separate strings for each one I needed to work with.

Thanks for all help :)
Topic archived. No new replies allowed.