Hi there. I am trying to make this code for a custom string class in the exercise:
Exercise 14.3.1. Rewrite the operator+ function as a global friend function, as just suggested. Review Chapter 13 if you need to do so. (Hint: No real change has to be made to the code other than the way the function is declared. You can still get away with having just one operator+ function.)
My original code supported this:
String b = a + "Kong"
but now I have to change it so it can also support:
1>c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(23): error C2803: 'operator +' must have at least one formal parameter of class type
1>c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(39): error C2679: binary '+' : no operator found which takes a right-hand operand of type 'String' (or there is no acceptable conversion)
1> c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(23): could be 'String operator +(char *,String *)'
1> while trying to match the argument list '(const char [12], String)'
1>c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(79): error C2511: 'String String::operator +(String *)' : overloaded member function not found in 'String'
1> c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(7) : see declaration of 'String'
1>
I am quite confused as to why this doesn't work... please help me.
You need to make your friend operator take a const char pointer, not just a regular pointer. You allso need to define an operator+ that takes a String too.
You need to make your friend operator take a const char pointer, not just a regular pointer. You allso need to define an operator+ that takes a String too.
I tried the first one, didn't work. But what do you mean by a operator+ that accepts a string too? Do you mean one that supports string and char? or one that supports just a string?
1>c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(75): error C2039: '+' : is not a member of 'String'
1> c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(7) : see declaration of 'String'
1>c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(75): error C2373: 'operator +' : redefinition; different type modifiers
1> c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(23) : see declaration of 'operator +'
1>c:\users\***\documents\visual studio 2010\projects\string_class\string_class\string_class.cpp(77): error C2664: 'String::cat' : cannot convert parameter 1 from 'const String' to 'char *'
Why don't you have to do the namespace thing for the const String operator + anymore? I thought it would have to be something like: String:: const String operator +
and why is it declared as a friend? I don't see it touching any private members of the class.
You have already in your class a conversion function from const char * to String. It is the constructor with parameter const char *. Did you define the constructor String( const char * )?
So when the compiler will see an expression as for example
String a( "Some String" );
a = a + "Another String";
it converts string literal "Another String" to an object of type String by calling constructor String( const char * ).
friend function does not belong to befriended class scope.
friend function does not belong to befriended class scope.
I know that, but what does the friend function access? It accesses private data members, but I don't see it accessing any private members of any class...
(Sorry for my mis-understanding, I've been learning c++ only for a month)