0

Using the MS command line C++ compiler, version as indicated in the output below, compiling the following fails with the compiler report as included:

// File: shared_memory.h

template <class Contents>
class SharedMemory {
public:
    ...
    operator Contents& () {
        ...
    }
    ...
    SharedMemory &operator= (Contents &contents) { // [EDIT] CONST REMOVED AT PARAMETER
        ...
    }
    ...
};

// File: shared_memory_test.cpp

class SharedAnimals: public SharedMemory <Animals> {
    ...
    using SharedMemory <Animals>::operator=;       // [EDIT] HAD TO BE ADDED
    ...
};                              // This is line 67 in the original code

int main (int argc, char** argv) {
    ...
    Animals animals (kind);
    ...
    sharedAnimals = animals;    // This is line 93 in the original code
    ...
}

// Compiler reports:

shared_memory_test.cpp
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
shared_memory_test.cpp(93): error C2679: binary '=': no operator found which takes a right-hand operand of type 'Animals' (or there is no acceptable conversion)
shared_memory_test.cpp(67): note: could be 'SharedAnimals &SharedAnimals::operator =(SharedAnimals &&)'
shared_memory_test.cpp(67): note: or       'SharedAnimals &SharedAnimals::operator =(const SharedAnimals &)'
shared_memory_test.cpp(93): note: while trying to match the argument list '(SharedAnimals, Animals)'

Can anyone give me a hint on what I am doing wrong?

[EDIT]

Indeed the answer at Assignment operator inheritance provided by user1810087 solved the problem.

While the answer is already there, the context of this question, namely using a template class, led me to believe that the error was connected to that, and did not look for questions relating to "assignment operator inheritance".

Since I may not be the only one running into this, I suggest this question be maintained.

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45

0 Answers0