So I have a vector class that looks a bit like this (most methods stripped out for clarity):
所以我有一个向量类,它看起来有点像这样(大多数方法为了清晰起见被去掉了):
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];
}
And at some point in my code I call this array subscript overload as follows:
在我的代码中,我将这个数组下标重载,如下所示:
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;
}
...
}
but I get this error at compile time:
但是我在编译时得到这个错误:
error: invalid types '<unresolved overloaded function type>[int]' for array subscript
Now, I have fiddled around with the signatures of the overload functions, adding and removing the reference symbols, adding and removing const, but I'm really just guessing here.
现在,我已经修改了重载函数的签名,添加和删除引用符号,添加和删除const,但这里我只是猜测。
What is the sensible way to write the array subscript operator overloads for a vector class for real numbers like this which allow us to do simple things like:
将数组下标运算符重载为一个向量类的方法是什么,这样的方法可以让我们做一些简单的事情,比如:
instance[i] = 5.7;
and
和
new_value = instance[j] + 17.3;
?
吗?
EDIT: the full class specification, as requested:
编辑:完整的类规格,按要求:
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();
};
1 个解决方案
#1
7
As commenters are pointing out, this error pops up when you try and call a function with brackets []
instead of parentheses ()
. This is exactly what is happening here, and wasn't obvious because I simplified the code example.
正如评论者指出的,当您尝试调用带有括号[]而不是圆括号()的函数时,这个错误就会出现。这正是这里所发生的事情,并不明显,因为我简化了代码示例。
In the question, I post and example function called func
- this was in fact a constructor of an inherited class (hence, rather than post all the code, I simplified.)
在这个问题中,我发布了一个名为func的示例函数——这实际上是一个继承类的构造函数(因此,我简化了代码,而不是发布所有代码)。
The base class contains all we need to know:
基类包含了我们需要知道的所有内容:
class D3Shape {
protected:
double l[6];
virtual void initilise_limits() = 0;
public:
virtual bool contains(D3Vector point) = 0;
vector<double> limits();
};
i.e. I had confused l
, the private member variable storing the double[6]
I was looking for, with limits()
, a function for retrieving them in a std::vector<double>
container. This was complicated by the fact that I was (successfully) using my real array-subscript-overloaded class on the same line, which confused me! The compiler error "column number" on the file was in fact pointing to the first character after the =
, further muddying the waters.
例如,我混淆了l,它是一个私有成员变量,用于存储我正在寻找的双[6],使用limit(),在std::vector
Many thanks to everyone who commented.
非常感谢大家的评论。
#1
7
As commenters are pointing out, this error pops up when you try and call a function with brackets []
instead of parentheses ()
. This is exactly what is happening here, and wasn't obvious because I simplified the code example.
正如评论者指出的,当您尝试调用带有括号[]而不是圆括号()的函数时,这个错误就会出现。这正是这里所发生的事情,并不明显,因为我简化了代码示例。
In the question, I post and example function called func
- this was in fact a constructor of an inherited class (hence, rather than post all the code, I simplified.)
在这个问题中,我发布了一个名为func的示例函数——这实际上是一个继承类的构造函数(因此,我简化了代码,而不是发布所有代码)。
The base class contains all we need to know:
基类包含了我们需要知道的所有内容:
class D3Shape {
protected:
double l[6];
virtual void initilise_limits() = 0;
public:
virtual bool contains(D3Vector point) = 0;
vector<double> limits();
};
i.e. I had confused l
, the private member variable storing the double[6]
I was looking for, with limits()
, a function for retrieving them in a std::vector<double>
container. This was complicated by the fact that I was (successfully) using my real array-subscript-overloaded class on the same line, which confused me! The compiler error "column number" on the file was in fact pointing to the first character after the =
, further muddying the waters.
例如,我混淆了l,它是一个私有成员变量,用于存储我正在寻找的双[6],使用limit(),在std::vector
Many thanks to everyone who commented.
非常感谢大家的评论。