Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 常量、转换和泛型数组问题_C++_Constants - Fatal编程技术网

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;