【问题标题】:unresolved overloaded function type -- array subscript overloading in C++未解决的重载函数类型——C++中的数组下标重载
【发布时间】:2013-01-19 23:54:26
【问题描述】:

所以我有一个看起来有点像这样的向量类(为了清楚起见,去掉了大多数方法):

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;

?

EDIT:完整的类规范,根据要求:

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

【问题讨论】:

  • 你有一个叫centre的函数吗?
  • 您的代码看起来不错,但 &lt;unresolved overloaded function type&gt; 看起来很可疑。你能发布一个完整的例子吗?
  • 按要求提供的完整类原型 :) -并且-哦,您的意思是函数而不是方法。不,没有代码可以看到的名为 center 的裸函数。
  • @tehwalrus, limits 也是?
  • 制作一个显示此错误的完整示例。您的代码中有错误,但它是您未显示的代码。在某个地方,您已经说服编译器 limitscentre 是一个函数。

标签: c++ operators


【解决方案1】:

正如评论者所指出的,当您尝试使用括号 [] 而不是括号 () 调用函数时,会弹出此错误。这正是这里发生的事情,并不明显,因为我简化了代码示例。

在问题中,我发布了名为 func 的示例函数 - 这实际上是继承类的构造函数(因此,我简化了代码,而不是发布所有代码。)

基类包含我们需要知道的所有内容:

class D3Shape {
  protected:
    double l[6];
    virtual void initilise_limits() = 0;
  public:
    virtual bool contains(D3Vector point) = 0;
    vector<double> limits();
};

即我混淆了l(存储我正在寻找的double[6] 的私有成员变量)和limits()(用于在std::vector&lt;double&gt; 容器中检索它们的函数)。这很复杂,因为我(成功地)在同一行上使用了我真正的数组下标重载类,这让我很困惑!文件上的编译器错误“列号”实际上指向了=之后的第一个字符,进一步搅浑水。

非常感谢所有评论的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多