0

I have written two custom classes, called A and B My B class has a member variable a, and it has a function:

void B::setA(A s)
{
    a = s;
}

However, this line is giving me an error:

'operator =' function is unavailable in 'A'.

From my understanding, this means somehow the compiler is not creating a default copy assignment function for my A class. However, I don't see why... the only constructor I've written in A looks like...

class A
{
    //...
public:
    A() {};
    //...
};

Now in truth, A and B are large classes with a lot of member variables, and the names aren't really A and B. Short of posting the entire source for both of these classes, could anyone tell me what types of things I might look for when trying to diagnose that error message? Just to be clear, I never use pointers or low-level features like that so, as far as I know, the default copy-assignment should work fine for class A.

edit:

Someone mentioned that one of the member variables of A might be to blame, so here are all of the types:

const std::string
static const bool
static std::map<int,std::string>
std::vector
std::map<int, C>

where C is a struct that I defined in the same header file that A is in. It just contains a few integers and strings.

user3281410
  • 502
  • 3
  • 14

2 Answers2

1

You have in class A a non-static data member declared as

const std::string

that is with the qualifier const. In this case the implicit copy assignment operator is defined by the compiler as deleted.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • That was it. Thanks. Could you point me to a reference that explains this maybe? I'm migrating to c++ from java, and I don't really understand what a const string has to do with a copy assignment operator. Is it because you can't assign to a const string? – user3281410 Mar 02 '14 at 17:21
  • @user3281410 `const` means immutable. Assigning a value to something constitutes mutating it. So yes, it is all down to the `const` data member. – juanchopanza Mar 02 '14 at 17:26
  • @user3281410 const is a lot like final in Java. – Christian Hackl Mar 02 '14 at 17:28
  • @@user3281410 From the C++ Standard "4 Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior" – Vlad from Moscow Mar 02 '14 at 17:35
1

const members don't mix well with copy assignment operators. Not only won't the compiler generate one for you, it would also be semantically hard to come up with an "assignment" which does not actually assign everything.

Nothing stops you from writing an assignment operator which does not assign to your const member, of course, but that would mean one of two things:

  1. The assigned object does not appear to the client as an identical copy of the source, which is not what one would except from a copy assignment operator, so that would be a case of operator-overloading abuse.
  2. The member variable does not have an observable effect to the outside. In that case, one must wonder why it exists in the first place, or rather why it is not mutable rather than const.
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62