Undefined operator?
-
I have a string class, where I defined the binary operator '+' in the following ways: class String:public List<char> { ... String& operator + (const char[]) const; String& operator + (const int) const; friend String& operator +(const char[], const String& ); friend String& operator +(const uint, const String& ); ... } along with an inherited method from the base class List as follows: List<TYPE>& operator + (const List<TYPE>& ) const; When I try to build the project, I get the following error: error C2678: binary '+' : no operator found which takes a left-hand operand of type 'String' (or there is no acceptable conversion) I am getting three identical errors from other parts of my program that were written in a similar manner. Does anyone know what this can be attributed to? Thanks, -Jeff -- modified at 8:03 Wednesday 16th August, 2006
-
I have a string class, where I defined the binary operator '+' in the following ways: class String:public List<char> { ... String& operator + (const char[]) const; String& operator + (const int) const; friend String& operator +(const char[], const String& ); friend String& operator +(const uint, const String& ); ... } along with an inherited method from the base class List as follows: List<TYPE>& operator + (const List<TYPE>& ) const; When I try to build the project, I get the following error: error C2678: binary '+' : no operator found which takes a left-hand operand of type 'String' (or there is no acceptable conversion) I am getting three identical errors from other parts of my program that were written in a similar manner. Does anyone know what this can be attributed to? Thanks, -Jeff -- modified at 8:03 Wednesday 16th August, 2006
The message is issued by the compiler that didn't find a match that satisfy the RIGHT operand, you said nothing about. Admitting it was another "String", the reasons is because, when you override a name all definition pertinent to that name are lost in the inner scope. In fact, since you override
operator+
inString
, any underlyingoperator+
inList
are anymore visible while accessingString
.
You should redeclare, inString
,friend String& operator+(const String& s)
{ return List<char>::operator+(s); }along with the other
operator+
variants you already declared.2 bugs found. > recompile ... 65534 bugs found. :doh:
-
The message is issued by the compiler that didn't find a match that satisfy the RIGHT operand, you said nothing about. Admitting it was another "String", the reasons is because, when you override a name all definition pertinent to that name are lost in the inner scope. In fact, since you override
operator+
inString
, any underlyingoperator+
inList
are anymore visible while accessingString
.
You should redeclare, inString
,friend String& operator+(const String& s)
{ return List<char>::operator+(s); }along with the other
operator+
variants you already declared.2 bugs found. > recompile ... 65534 bugs found. :doh:
I attempted that piece of code, and received an error like "Using non-static member of List<char>" However, your response inspired me to try other things that were close to your solution, and the one that finally made the problem go away was simply to redefine the operation in the string class like:
String& String::operator +(const String&) const { <code here> }
I guess what I am confused about, is the following: In the following code, why isn't the List operator + equivalent to the String operator + as defined above from the compiler's perspective? Is there a way to make them equivalent, perhaps by using another method of definition?
template <class TYPE>
class List<TYPE> {
...
public:
List<TYPE>& operator +(const List<TYPE>& ) const;
...
}class String:public List<char> {
}Thanks, -Jeff -- modified at 14:36 Wednesday 16th August, 2006
-
I attempted that piece of code, and received an error like "Using non-static member of List<char>" However, your response inspired me to try other things that were close to your solution, and the one that finally made the problem go away was simply to redefine the operation in the string class like:
String& String::operator +(const String&) const { <code here> }
I guess what I am confused about, is the following: In the following code, why isn't the List operator + equivalent to the String operator + as defined above from the compiler's perspective? Is there a way to make them equivalent, perhaps by using another method of definition?
template <class TYPE>
class List<TYPE> {
...
public:
List<TYPE>& operator +(const List<TYPE>& ) const;
...
}class String:public List<char> {
}Thanks, -Jeff -- modified at 14:36 Wednesday 16th August, 2006
Skippums wrote:
I attempted that piece of code, and received an error like "Using non-static member of List"
OOPS: In fact I did a mistake in writing the function: List::operator+ returns a List, not a String as i did.
Skippums wrote:
Is there a way to make them equivalent, perhaps by using another method of definition?
No, that's basic C++ scope overriding.
class A
{
void f1(int) { ... }
void f1(double) { ... }
};class B: public A
{
void f1(double) { ... }
};It is not
B::f1(double)
overridingA::f1(double)
. It isB::f1
maskingA::f1
, whateverf1
represent inA
orB
. You can have as manyf1
variant you want inA
, but as you declaref1
to do whatever thing inB
,f1
inA
is "masked, when working withB
scope". -- modified at 11:32 Thursday 17th August, 20062 bugs found. > recompile ... 65534 bugs found. :doh: