赋值运算符 C++
我正在尝试实现赋值运算符,但不断收到一些错误:
calendar.cpp:25: error: prototype for ‘lab2::Calendar<T>& lab2::Calendar<T>::operator=(lab2::Calendar<K>)’ does not match any in class ‘lab2::Calendar<T>’
calendar.h:19: error: candidate is: template<class T> template<class K> lab2::Calendar& lab2::Calendar::operator=(lab2::Calendar<K>)
make: *** [calendar.o] Error 1
在尝试不同的解决方案时,我还遇到了“&”之前的预期构造函数、析构函数或类型转换。
日历的日期类型为 T,即日历中的日期可以不是公历日期。但我还希望能够从具有其他日期类型的日历分配给日历。
这是我的声明和实施。
//calendar.h
template <typename T>
class calendar {
//...
template <typename K> Calendar& operator=(const Calendar<K> c_other);
//...
}
//calendar.cpp
//...
template <typename T, typename K>
Calendar<T>& Calendar<T>::operator=(const Calendar<K> c_other) {};
//...
将不胜感激任何帮助。
I'm trying to implement an assignment operator but I keep getting some errors:
calendar.cpp:25: error: prototype for ‘lab2::Calendar<T>& lab2::Calendar<T>::operator=(lab2::Calendar<K>)’ does not match any in class ‘lab2::Calendar<T>’
calendar.h:19: error: candidate is: template<class T> template<class K> lab2::Calendar& lab2::Calendar::operator=(lab2::Calendar<K>)
make: *** [calendar.o] Error 1
I've also encountered expected constructor, destructor, or type conversion before '&'
, when trying different solutions.
The calendar has date type T, i.e. the dates within the calendar can be other than gregorian. But I also want to be able to assign to a calendar from a calendar with another date type.
Here is my declaration and implementation.
//calendar.h
template <typename T>
class calendar {
//...
template <typename K> Calendar& operator=(const Calendar<K> c_other);
//...
}
//calendar.cpp
//...
template <typename T, typename K>
Calendar<T>& Calendar<T>::operator=(const Calendar<K> c_other) {};
//...
Would appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
恐怕您使用的模板存在一些问题。
首先,您想使用引用传递,而不是值传递。这样,您就可以控制创建参数对象。
然后,真正的问题来了:
这是在源文件中,因此其余代码将找不到专业化。您应该将该方法的实现移至头文件中,这样,所有调用站点都可以替换自己的模板参数并专门化类和赋值运算符。
I'm afraid there's some problem with your use of templates.
For first you want to use reference passing, instead of value. That way, you are in control to create the parameter object.
Then, the real problem comes:
This is in the source file so the rest of the code won't find the specialisation. You should move the implementation of the method in to the header file, that way, all the calling sites can substitute their own template parameters and specialize the class and the assignment operator.
除了其他人所说的之外,类模板的成员函数模板定义的正确语法是:
Besides what others said, the correct syntax for a definition of a member function template of a class template is:
将
operator=
的主体(实现)移至 .h 文件。更多信息请参见此处。
更新:如果有时某些声明在源文件中声明时有效,请确保该类的所有对象都位于
同一源文件同一翻译单元中。Move body(implementation) of your
operator=
to .h file.More information is here.
UPDATE: If sometimes some declarations works when they declared in a source file, be sure the all objects of that class are in
same source filesame translation unit.考虑一下这段代码。我称其为不对称赋值运算符,因为模板参数不同。如果隐式转换不存在,您将必须自己完成转换工作。您可以使用以下代码尝试一下:
Condider this code. I call this an asymmetrical assignment operator since the template arguments are different. If an implicit conversion does not exist you will have to do the conversion work yourself. You can try it with this code :