模板类

时间:2019-04-04 07:16:08

标签: c++ templates operator-overloading assignment-operator

使用MS命令行C ++编译器(如下面的输出所示),如果包含以下编译器报告,则编译以下内容将失败:

// 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)'

谁能给我一个关于我做错了什么的提示?

[编辑]

实际上是由user1810087提供的Assignment operator inheritance处的答案已解决。

虽然答案已经存在,但是该问题的上下文(即使用模板类)使我相信错误与之相关,并且没有寻找与“赋值运算符继承”有关的问题。

由于我可能不是唯一一个遇到这个问题的人,所以我建议保留这个问题。

0 个答案:

没有答案