Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Undefined operator?

Undefined operator?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skippums
    wrote on last edited by
    #1

    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

    E 1 Reply Last reply
    0
    • S Skippums

      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

      E Offline
      E Offline
      Emilio Garavaglia
      wrote on last edited by
      #2

      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+ in String, any underlying operator+ in List are anymore visible while accessing String.
      You should redeclare, in String,

      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:

      S 1 Reply Last reply
      0
      • E Emilio Garavaglia

        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+ in String, any underlying operator+ in List are anymore visible while accessing String.
        You should redeclare, in String,

        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:

        S Offline
        S Offline
        Skippums
        wrote on last edited by
        #3

        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

        E 1 Reply Last reply
        0
        • S Skippums

          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

          E Offline
          E Offline
          Emilio Garavaglia
          wrote on last edited by
          #4

          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) overriding A::f1(double). It is B::f1 masking A::f1, whatever f1 represent in A or B. You can have as many f1 variant you want in A, but as you declare f1 to do whatever thing in B, f1 in A is "masked, when working with B scope". -- modified at 11:32 Thursday 17th August, 2006

          2 bugs found. > recompile ... 65534 bugs found. :doh:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups