C++ 常量、转换和泛型数组问题
我在这些方面有一个问题:C++ 常量、转换和泛型数组问题,c++,constants,C++,Constants,我在这些方面有一个问题: const int* index = faceArray[f].vertices; const Vector3& A = vertexArray[index[0]]; const Vector3& B = vertexArray[index[1]]; const Vector3& C = vertexArray[index[2]]; faceNormal[f] = Vector3::Cross(B - A, C - A).Normalize(
const int* index = faceArray[f].vertices;
const Vector3& A = vertexArray[index[0]];
const Vector3& B = vertexArray[index[1]];
const Vector3& C = vertexArray[index[2]];
faceNormal[f] = Vector3::Cross(B - A, C - A).Normalize();
当我尝试编译时,我得到一个错误:
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const MyArray<Data>' (or there is no acceptable conversion)
with [Data=Vector3]
c:\...\projects\haziarnyek\hazi\hazi.cpp(502): could be 'Vector3 &MyArray<Data>::operator [](const int &)'
with [Data=Vector3]
while trying to match the argument list '(const MyArray<Data>, int)'
with [Data=Vector3]
我认为这是你给出的第一个片段的上下文
void f(const MyArray<Vector3> &vertexArray) {
...
}
您应该定义运算符[]的常量版本:
const x& operator[] (unsigned idx) const;
除非你告诉我们faceNormal是如何定义的,否则我们可能无法帮助你。如果你让人们更容易帮助你,你就更有可能得到一个有用的答案。下一次,不要让我们太多地了解你的想法<代码>:-)
const Data& operator[] (int i) const {
...
}
const x& operator[] (unsigned idx) const;