提问人:Jacques de Hooge 提问时间:4/4/2019 最后编辑:Jacques de Hooge 更新时间:4/5/2019 访问量:91
模板类中的重载赋值运算符出现问题
Problem overloading assignment operator in template class
问:
使用 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 运算符继承的答案解决了这个问题。
虽然答案已经存在,但这个问题的上下文,即使用模板类,使我相信错误与此有关,并且没有寻找与“赋值运算符继承”相关的问题。
由于我可能不是唯一一个遇到这个问题的人,我建议保留这个问题。
答: 暂无答案
下一个:模板类赋值运算符
评论
SharedAnimals
...