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

发布于 2024-08-24 07:59:12 字数 1104 浏览 8 评论 0原文

第二个函数给出错误 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;
}

the second function gives error C2803 http://msdn.microsoft.com/en-us/library/zy7kx46x%28VS.80%29.aspx : 'operator ,' must have at least one formal parameter of class type. any clue?

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

長街聽風 2024-08-31 07:59:12

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

In C++ you can't overload operator if at least one of the parameters of it is not a custom made class or something not primitive like enum. You can't overload + for int types and similarly you can't overload , for double types.

二货你真萌 2024-08-31 07:59:12

将其更改为:

Sequence<double> operator,(const Sequence<double>& a, const double& b)
{
    Sequence<double> seq(a);
    seq.push_back(b);
    return seq;
}

或(基于这篇文章< /a>):

Sequence<double> operator,(Sequence<double> seq, const double& b)
{
    seq.push_back(b);
    return seq;
}

Change that to:

Sequence<double> operator,(const Sequence<double>& a, const double& b)
{
    Sequence<double> seq(a);
    seq.push_back(b);
    return seq;
}

or (based on this article):

Sequence<double> operator,(Sequence<double> seq, const double& b)
{
    seq.push_back(b);
    return seq;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文