赋值运算符 C++

发布于 2024-12-11 05:28:20 字数 918 浏览 0 评论 0原文

我正在尝试实现赋值运算符,但不断收到一些错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风无影 2024-12-18 05:28:20

恐怕您使用的模板存在一些问题。
首先,您想使用引用传递,而不是值传递。这样,您就可以控制创建参数对象。

template <typename K> Calendar& operator=(const Calendar<K>& c_other);

然后,真正的问题来了:

template <typename T, typename K>
Calendar<T>& Calendar<T>::operator=(const Calendar<K>& c_other) {};

这是在源文件中,因此其余代码将找不到专业化。您应该将该方法的实现移至头文件中,这样,所有调用站点都可以替换自己的模板参数并专门化类和赋值运算符。

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.

template <typename K> Calendar& operator=(const Calendar<K>& c_other);

Then, the real problem comes:

template <typename T, typename K>
Calendar<T>& Calendar<T>::operator=(const Calendar<K>& c_other) {};

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.

放肆 2024-12-18 05:28:20

除了其他人所说的之外,类模板的成员函数模板定义的正确语法是:

template <typename T>
template <typename K>
Calendar<T>& Calendar<T>::operator=(const Calendar<K>& c_other)
{
    // your code here
}

Besides what others said, the correct syntax for a definition of a member function template of a class template is:

template <typename T>
template <typename K>
Calendar<T>& Calendar<T>::operator=(const Calendar<K>& c_other)
{
    // your code here
}
三生路 2024-12-18 05:28:20

operator= 的主体(实现)移至 .h 文件。

因为模板是在需要时编译的,所以这会强制
多文件项目的限制:实施(定义)
模板类或函数的名称必须与它的同一个文件中
宣言。这意味着我们不能将接口分开
单独的头文件,并且我们必须同时包含接口和
在使用模板的任何文件中实现。

更多信息请参见此处

更新:如果有时某些声明在源文件中声明时有效,请确保该类的所有对象都位于同一源文件同一翻译单元中。

Move body(implementation) of your operator= to .h file.

Because templates are compiled when required, this forces a
restriction for multi-file projects: the implementation (definition)
of a template class or function must be in the same file as its
declaration. That means that we cannot separate the interface in a
separate header file, and that we must include both interface and
implementation in any file that uses the templates.

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 file same translation unit.

ペ泪落弦音 2024-12-18 05:28:20
template<class T>
class Calendar
{
public:
    T i;
    Calendar(const T & i_in):i(i_in){}
    template <class T2>
    Calendar <T>& operator=(const Calendar<T2> &);
};

template <class T>
 template <class T2>
Calendar<T>& Calendar<T>::operator=(const Calendar<T2>& t2)
{
 if (this == (void*) &t2) //avoid self assignment
  return *this; 

 this->i = t2.i; //implicit conversion here for example double to int or vice versa
 return *this;  
}

考虑一下这段代码。我称其为不对称赋值运算符,因为模板参数不同。如果隐式转换不存在,您将必须自己完成转换工作。您可以使用以下代码尝试一下:

int main() {
    Calendar<double> tmp(2.0);
    std::cout << "Tmp i : " << tmp.i << std::endl;
    Calendar<int> tmp_2(1);
    std::cout << "tmp_2 i : " << tmp_2.i << std::endl;
    tmp = tmp_2;
    std::cout << "Tmp i : " << tmp.i << std::endl;
    return 0;
}
template<class T>
class Calendar
{
public:
    T i;
    Calendar(const T & i_in):i(i_in){}
    template <class T2>
    Calendar <T>& operator=(const Calendar<T2> &);
};

template <class T>
 template <class T2>
Calendar<T>& Calendar<T>::operator=(const Calendar<T2>& t2)
{
 if (this == (void*) &t2) //avoid self assignment
  return *this; 

 this->i = t2.i; //implicit conversion here for example double to int or vice versa
 return *this;  
}

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 :

int main() {
    Calendar<double> tmp(2.0);
    std::cout << "Tmp i : " << tmp.i << std::endl;
    Calendar<int> tmp_2(1);
    std::cout << "tmp_2 i : " << tmp_2.i << std::endl;
    tmp = tmp_2;
    std::cout << "Tmp i : " << tmp.i << std::endl;
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文