1

I've ran into some trouble while trying to solve a simple problem for my C++ class.

I have two classes: Component, an abstract class from which I inherit other classes and List, a class that has a list of Components (it is not a template class). I want to overload the operator+. so that when I "add" two components, it will return a List containing both Components.

I've done so using this, which shows no errors:

friend List operator +(Component &c1, Component &c2) {
        List l;
        l.push(c1);
        l.push(c2);
        return l;
    }

However, when I try to "add" two objects of classes that are inherited from Component, I get the following error:

no match for 'operator+' in 'c1 + c2'

Here is how I add the objects:

Inherited1 c1(1, 2, 3);
Inherited2 c2(1, 3.2, 10);
List l1 = c1+c2;
Gregor Menih
  • 5,036
  • 14
  • 44
  • 66
  • 1
    [This works](http://coliru.stacked-crooked.com/a/792b5213c9ec09d4) and it's fundamentally what you've shown. There's definitely an element of a non-reproducible problem here. – chris May 25 '14 at 15:32
  • 1
    This is a bad idea. Unless you are actually implementing a DSL you should preserve the arithmetic meaning of operators. – pmr May 25 '14 at 15:32
  • `operator +` takes `Component&`, you are sending `Inherited`, how will it work? – Rakib May 25 '14 at 15:33
  • @RakibulHasan, Because it's a derived class. – chris May 25 '14 at 15:34
  • 1
    you need to add definition of function out of the class . – uchar May 25 '14 at 15:37
  • @omid, My example does not, but still works. – chris May 25 '14 at 15:39
  • @chris But he say Component is abstract class. Maybe is problem here. – Nejc Galof May 25 '14 at 15:41
  • @NejcGalof, Good point. Allow me to amend my example: http://coliru.stacked-crooked.com/a/23a13931b2680d26 – chris May 25 '14 at 15:44
  • @chris I think Component isn't abstract class but is derived class from some abstract class. – Nejc Galof May 25 '14 at 15:49
  • @NejcGalof, I don't get that impression, but it shouldn't make a difference either way. Of course if the OP produced an MCVE instead of me, we wouldn't be having this discussion. – chris May 25 '14 at 15:50
  • @omid you are right I thats how it would work. – Nik May 25 '14 at 16:12

1 Answers1

3

The friend function is not defined outside class hence the compiler is not able to find operator+. The friend func operator+ should be defined outside the List class.

As you have not posted the complete structure of classes, I have assumed and tried this:

#include <iostream>
#include <list>

using namespace std;


class Component{

    public:


};

class List{

    list<Component> l;
public:

   void push(Component& c)
   {
       l.push_back(c);
   }
  friend List operator +(Component &c1, Component &c2);

};

class Inherited1:public Component{

    public:
    Inherited1(int x,int y,int z){}

};

class Inherited2:public Component{

    public:
     Inherited2(int x,int y,int z){}  


};

 List operator +(Component &c1, Component &c2) {
        List l;
        l.push(c1);
        l.push(c2);
        cout<<"called"<<endl;
        return l;
    }



int main()
{

    Inherited1 c1(1, 2, 3);
    Inherited2 c2(1, 3.2, 10);
    List l1 = c1 + c2;

    return 0;
}

I assume that you have operator+ func inside your List class as shown below because of which you are getting error :

error: no match for ‘operator+’ (operand types are ‘Inherited1’ and ‘Inherited2’) List l1 = c1 + c2;


class List{

    list<Component> l;
public:

   void push(Component& c)
   {
       l.push_back(c);
   }
  friend  List operator +(Component &c1, Component &c2) {
        List l;
        l.push(c1);
        l.push(c2);
        cout<<"called"<<endl;
        return l;
    }

};
Nik
  • 1,294
  • 10
  • 16