gpt4 book ai didi

c++ - 如何在header中声明是struct中的重载运算符?

转载 作者:行者123 更新时间:2023-11-28 05:05:13 25 4
gpt4 key购买 nike

<分区>

我有一个结构,其中运算符模板类型。要在标题中正确声明此结构。编写如下代码

头文件

struct add_positive {
template<typename T>
T operator()(T value, const T& now);
};

.cpp 文件

template<typename T>
add_positive add_positive::operator()(T value, const T& now) {
if (now >= 0) {
return value + now;
}

return value;
}

但是编译时出现如下错误:

error: prototype for ‘add_positive add_positive::operator()(T, const T&)’ does not match any in class ‘add_positive’
add_positive add_positive::operator()(T value, const T& now) {
error: candidate is: template<class T> T add_positive::operator()(T, const T&)
T operator()(T value, const T& now);

不明白我做错了什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com