第二個函數給出錯誤C2803 http://msdn.microsoft.com/en-us/library/zy7kx46x%28VS.80%29.aspx:'運算符'必須至少有一個類類型的形式參數。任何線索?C++覆蓋全局運算符逗號給出錯誤
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;
}
您不能重載內置類型的運算符。而你一再試圖超負荷運動的方法被誤導了 - 這不是一個神奇的子彈。 – 2010-03-09 01:57:12
@尼爾,你的評論太棒了。我喜歡它們xD – 2010-03-09 02:56:48
這是一個魔法子彈,它總是將你射向腳下! – Potatoswatter 2010-03-09 03:23:41