gpt4 book ai didi

C++ 覆盖全局运算符逗号给出错误

转载 作者:行者123 更新时间:2023-11-30 01:31:56 25 4
gpt4 key购买 nike

第二个函数给出错误 C2803 http://msdn.microsoft.com/en-us/library/zy7kx46x%28VS.80%29.aspx :“operator”必须至少有一个类类型的形式参数。有什么线索吗?

template<class T,class A = std::allocator<T>> 
class Sequence : public std::vector<T,A> {
public:
Sequence<T,A>& operator,(const T& a) {
this->push_back(a);
return *this;
}
Sequence<T,A>& operator,(const Sequence<T,A>& a) {
for(Sequence<T,A>::size_type i=0 ; i<a.size() ; i++) {
this->push_back(a.at(i));
}
return *this;
}
};

//this works!
template<typename T>
Sequence<T> operator,(const T& a, const T&b) {
Sequence<T> seq;
seq.push_back(a);
seq.push_back(b);
return seq;
}

//this gives error C2803!
Sequence<double> operator,(const double& a, const double& b) {
Sequence<double> seq;
seq.push_back(a);
seq.push_back(b);
return seq;
}

最佳答案

在 C++ 中,如果运算符的至少一个参数不是自定义类或非像枚举这样的原始类型,则不能重载运算符。您不能为 int 类型重载 +,同样,您也不能为 double 类型重载 ,

关于C++ 覆盖全局运算符逗号给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2406144/

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