string test (const string &hey) //what dose const do here
{
return hey;
}
string t (string &hey)
{
return hey;
}
int main ()
{
string h = test("hi");
cout << h;
h = t("hi"); //doesnt work
cout<<h;
}
In function 'int main()':
17:12: error: invalid initialization of non-const reference of type 'std::string& {aka std::basic_string<char>&}' from an rvalue of type 'const char*'
7:8: note: in passing argument 1 of 'std::string t(std::string&)'
Initialization? Lets give a string object then:
1 2
const std::string foo( "hi" );
h = t( foo );
Not better:
In function 'int main()':
18:13: error: invalid initialization of reference of type 'std::string& {aka std::basic_string<char>&}' from expression of type 'const string {aka const std::basic_string<char>}'
7:8: note: in passing argument 1 of 'std::string t(std::string&)'