So, in the course of trying to write my own matrix-class (as an exercise, if nothing else), I ran across the following problem, exemplified in a dummy class:

class dummy
{
  public:
  int a;
  int b;
  dummy(int a, int b): a(a), b(b) {};
  
  dummy& operator=(dummy &rhs)
  {
    a=rhs.a; b=rhs.b;
    return *this;
  }
    
  dummy operator+(dummy &rhs)
  {
    dummy out(a+rhs.a,b+rhs.b);
    return out;
  }
  
};

Now, there are two versions of main, one that gives an error, and one that doesn't:

error-version:

int main()
{
dummy d1(1,1), d2(0,0);
dummy d3(2,2);
 d3=d1+d2;

std::cout << d3.a;

return 0;
}

This gives the following message:
main.cpp:55:error: no match for ‘operator=’ in ‘d3 = d1.dummy::operator+(((dummy&)(& d2)))’
main.cpp:25: note: candidates are: dummy& dummy::operator=(dummy&)

However, if I change the code slightly:

int main()
{
dummy d1(1,1), d2(0,0);

dummy  d3=d1+d2;

std::cout << d3.a;

return 0;
}

it works fine.

I also don't get any errors from

int main()
{
dummy d1(1,1), d2(0,0);

  d1=d2;

std::cout << d3.a;

return 0;
}

I hope I'm keeping inside the posting procedures, at least I tried my best to.

C++ doesn't allow the binding of temporary objects to non-const references. Your overloaded operator+ is returning a temporary object, which you then try to pass as a non-const reference to the overloaded operator=.

You can fix it by being const correct (a good idea anyway):

dummy& operator=(const dummy &rhs)

Note that const should generally be used if you aren't modifying the parameter. That guideline usually makes problems like this one go away.

thanks =) that was quick! I'm pretty sure that c++ quirk was not mentioned in the introductory texts I've been through, so I won't feel bad about having made that error.
(and yeah, adding 'const' did make the error go away, both for the dummy and the matrix class).
=)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.