0

I have a problem with assigning the same objects with multiple inheritance which also have diamond problem. Here is the skeleton code of my project.

H.h

 class H
 {
     protected: 
        int a;
        int b;
        int c;     
     public:
        H();
        H(int a);
        //Setter and getters    
 }; 

Y.h

 class Y : virtual H
 {
      public:
          Y();
          Y(int a);

  };

D.h

  class D : virtual H
  {
      public:
          D();
          D(int a);

  };

An.h

  class An : Y , D
  {

     public:
         An();
         An(int a);
  };

I would like to assign one An object to another. But i get this error: error C2582: 'operator =' function is unavailable in 'An' I searched google but found nothing. I am working on Visual Studio 2010

It works on Y or D or H like :

 int main()
 {
     Y *a = new Y[4];
     Y *b = new Y[4];
     a[0] = b[0];//this is not the problem
 }

Main.cpp

 int main()
 {
     An *a = new An[4];
     An *b = new An[4];
     a[0] = b[0];//this is the problem
 }

How can i solve this problem. Thanks in advance.

COvayurt
  • 827
  • 2
  • 11
  • 36
  • Are you sure you didn't mean `An *a = new An(3);`? That you be the idiomatic way of instantiating data dynamically. Your code is trying to assign an object of type `An` that comes from the static construction `An(3)` to an object of type `An*`. An assignment operator has not been defined between these two incompatible types. As a (bad) fix, you could also try `An *a = &An(3);` – Sharadh May 07 '14 at 09:50
  • Ok now i edited it.My fault.I forgot to write it properly.It supposed to be An *a = new An(3); ok then the problem is still a = b; on the other hand i can to y1 = y2; when i define them like An – COvayurt May 07 '14 at 09:56
  • Please check your error again. It is very, very unlikely that assigning one pointer to another causes an issue. Could you check if the line the error refers to is indeed `a = b;`? – Sharadh May 07 '14 at 10:04
  • I don't know if it makes difference, the actual line is like : An *temp = new An[10]; An *a = new An(3); temp[0] = a; and it is compiler error, not runtime. – COvayurt May 07 '14 at 10:07
  • Ah. `temp` is of type `An*`, but `temp[0]`, expanded as `*(temp+0)` is of type `An` because of that dereference. You're trying to assign a `An*` on your RHS to a `An` on your LHS. – Sharadh May 07 '14 at 10:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52197/discussion-between-covayurt-and-sharadh) – COvayurt May 07 '14 at 11:25

2 Answers2

0

Do you mean this?

 An *a = new An(3);
 An *b = new An(4);
 a = b;//this is not a problem

In your program,

AN *a = An(3); // not possible

is not psssible as lhs is An * and rhs is An The following is also fine:

 An a = An(3);
 An b = An(4);
 a = b;
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • It is not a problem when you inherit only one class. It is no problem assigning Y class like after initialize it : y1 = y2; but it is problem when you trying to do with An. – COvayurt May 07 '14 at 09:58
0

I found the solution. The code should be like this :

 class A {
   private:
        A& operator=(const A& a){}
 };

 class B : public A {
    public:
       // try the following line to resolve the error
       // void operator=(const B& b){}
 };

  int main() {
    B b1;
    B b2;
    b1 = b2;   // C2582
 }

When you define the

 void operator=(const B& b){}

into the An class as public. The problem solved.

COvayurt
  • 827
  • 2
  • 11
  • 36