【发布时间】:2015-06-27 09:50:59
【问题描述】:
我已经制作了自己的矢量模板,operator[] 的一部分:
template <class T>
T& vector<T>::operator[](unsigned int index)
{
return m_Data[index];
}
我在类复制构造函数中使用这段代码
Track::Track(const Track& src)
{
for(unsigned int i = 0; i < src.sorted.size(); i++)
AddCar(src.sorted[i]->rz, src.sorted[i]->owners.back()->name, src.sorted[i]->owners.back()->surname);
}
我收到一个错误'no match for operator[] (operands types are const vector<Track*> and unsigned int)'
我试图用const 关键字重载operator[]:
const T& operator[](unsigned int);
但这给了我一个不同的错误:const T& operator[](unsigned int); cannot be overloaded
这可能是什么原因?
【问题讨论】:
-
另一个错误是什么?
-
我猜当您将
constmodifier 添加到您的operator[]时,您应该返回const T&,这是您的第二个错误 -
我试过这个 const T& operator[](unsigned int);编译器说“const T& operator[](unsigned int); 不能重载”
-
@lllook ...当您将 const 修饰符添加到您的 operator[]...
标签: c++ vector operator-overloading