未解析的重载函数类型 – C中的数组下标重载
发布时间:2020-12-16 09:42:01 所属栏目:百科 来源:网络整理
导读:所以我有一个看起来有点像这样的矢量类(大多数方法为了清晰起见而被剥离): class D3Vector { private: double _values[3]; public: const double operator[](const int index) const; double operator[](const int index);};double D3Vector::operator[](co
所以我有一个看起来有点像这样的矢量类(大多数方法为了清晰起见而被剥离):
class D3Vector { private: double _values[3]; public: const double& operator[](const int index) const; double& operator[](const int index); }; double& D3Vector::operator[](const int index) { assert(index >= 0 && index < 3); return _values[index]; } const double& D3Vector::operator[](const int index) const { assert(index >= 0 && index < 3); return _values[index]; } 在我的代码中的某些时候,我调用此数组下标重载如下: void func(D3Vector centre,double radius) { double limits[6]; int i; for (i = 0; i < 3; i++) { // both these lines cause the error... limits[i] = centre[i] - radius; limits[i + 3] = centre[i] + radius; } ... } 但我在编译时遇到这个错误: error: invalid types '<unresolved overloaded function type>[int]' for array subscript 现在,我已经摆弄了重载函数的签名,添加和删除了引用符号,添加和删除const,但我真的只是在这里猜测. 为这样的实数编写向量类的数组下标运算符重载的合理方法是什么,它允许我们做简单的事情,如: instance[i] = 5.7; 和 new_value = instance[j] + 17.3; ? 编辑:完整的类规范,根据要求: class D3Vector { private: double _values[3]; public: // constructors - no args inits to 0.0 D3Vector(); D3Vector(const double x,const double y,const double z); // binary + and -: D3Vector operator+(const D3Vector& right); D3Vector operator-(const D3Vector& right); // unary -,reverses sign of components: D3Vector operator-(); // binary *,scales components. D3Vector operator*(const double scale); // the same,as self-assignment operations: D3Vector& operator+=(const D3Vector& right); D3Vector& operator-=(const D3Vector& right); D3Vector& operator*=(const double scale); // subscript operator,for member data access. const double& operator[](const int index) const; double& operator[](const int index); // dot product: double dot(D3Vector& right); // cross product: D3Vector cross(D3Vector& right); // shortcut to vector length: double mod(); // faster way of getting length squared: double mod_squared(); }; 解决方法
正如评论者指出的那样,当您尝试使用方括号[]而不是括号()调用函数时,会弹出此错误.这正是这里发生的事情,并不是很明显,因为我简化了代码示例.
在这个问题中,我发布了一个名为func的示例函数 – 这实际上是一个继承类的构造函数(因此,而不是发布所有代码,我简化了.) 基类包含我们需要知道的所有内容: class D3Shape { protected: double l[6]; virtual void initilise_limits() = 0; public: virtual bool contains(D3Vector point) = 0; vector<double> limits(); }; 即我混淆了l,私有成员变量存储了我正在寻找的双[6],带有limits(),一个用于在std :: vector< double>中检索它们的函数.容器.这是因为我(成功)在同一行上使用我的实际数组下标重载类这一事实使我感到困惑!文件上的编译器错误“列号”实际上指向=之后的第一个字符,进一步混淆了水域. 非常感谢所有评论的人. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |