操作系统:Windows 7 64位
IDE:Qt创建者5.7.0(32位)
编译:MinGW的5.3.0(32位)
C++,编译时间错误 “不匹配关于 '操作符[]'(操作数的类型是 'SomeClass的*' 和 'SomeClass的')”
作为前言,这是一项工作,帮助我了解了一些三分球/班力学,从而bool no = 1;
,我不想用<vector>
小号也没有任何其他easyToUse
工具。
我想通过一个[指针数组someClass
类型对象]作为函数的参数。 整个代码太长,为了将它粘贴在这里,我只会发布与错误相关的部分的简化版本。当你写void theFunction(someClass *arClassesIn, unsigned int arElementsIn);
,arClassesIn
是指向someClass
类型的对象
class someClass
{
public:
someClass(){;}
someClass(unsigned int classConstructorArgIn);
~someClass(){;}
}
someClass:someClass(unsigned int classConstructorArgIn)
{
//does stuff
}
void theFunction(someClass *arClassesIn, unsigned int arElementsIn);
int main()
{
unsigned int arElements = someOperationsResult; //cant really write down how I calculate this value, you can test it with a const value, if you have to
someClass *arClasses[arElements] = {0}; //array of pointers to instances of someClass (allegedly!)
theFunction(arClasses[arElements], arElements); //BTW::I had to write the argument this way, otherwise it says the definition doesnt match any declaration
//Furthermore, I dont understand what kind of value I should actually type in there, whether the index of the first element (0) or the size of the array (arElements)
return 0;
}
void theFunction(someClass *arClassesIn, unsigned int arElementsIn)
{
unsigned int classConstructorArg = 0;
for (unsigned int i = 0; i < arElements; i++)
{
classConstructorArg = someOtherOperationsResult;
arClassesIn[(someClass)i] = new someClass(classConstructorArg); //ERROR::"no match for 'operator[]' (operand types are 'someClass*' and 'someClass')"
//For some reason that I dont understand, I had to cast the [i]ndex to (someClass) type, otherwise I get a whole bunch of errors about type conversion
}
}
*我不想使用或任何其他easytoUse工具* - 无论工具是否“容易”,您仍然必须编写该程序。这并不像使用'vector'会神奇地为你编写程序。 –
PaulMcKenzie
@PaulMcKenzie为什么,我没写过? ''是否应该使数组的使用更容易和更安全,还是我误解了? –
真的,即使你去掉了一个不影响错误的事情,也可能是你最终得到了类classClass {}; someClass obj = new someClass();'。它给你与索引使用'i'相同的错误(这是更合乎逻辑的方法,正如你所说的,并且这两个和铸造'someClass'给出错误,所以......)这就是说,它是两行使其更容易看出它为什么失败。 – chris