2016-07-30 85 views
-1

操作系统: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 
    } 
} 
+2

*我不想使用或任何其他easytoUse工具* - 无论工具是否“容易”,您仍然必须编写该程序。这并不像使用'vector'会神奇地为你编写程序。 – PaulMcKenzie

+0

@PaulMcKenzie为什么,我没写过? ''是否应该使数组的使用更容易和更安全,还是我误解了? –

+1

真的,即使你去掉了一个不影响错误的事情,也可能是你最终得到了类classClass {}; someClass obj = new someClass();'。它给你与索引使用'i'相同的错误(这是更合乎逻辑的方法,正如你所说的,并且这两个和铸造'someClass'给出错误,所以......)这就是说,它是两行使其更容易看出它为什么失败。 – chris

回答

1

下面的代码(相关部分)。 但是,它看起来像你想传递一个指针数组。所以,你必须改变你的函数:

void theFunction(someClass **arClassesIn, unsigned int arElementsIn); 
// Or 
void theFunction(someClass *arClassesIn[], unsigned int arElementsIn); 

然后,你可以这样调用:

theFunction(arClasses, arElements); 

而且还通过

arClassesIn[i] = new someClass(classConstructorArg); 

更换

arClassesIn[(someClass)i] = new someClass(classConstructorArg); 

什么是你的代码错误:

1.

theFunction(arClasses[arElements], arElements); 

arClasses[arElements]是无效的。 arClasses是大小为arElements的数组,因此有效索引范围从0arElements - 1。访问任何其他索引(如arElements)可能会导致崩溃。

2.

arClassesIn[(someClass)i] = new someClass(classConstructorArg); 

由于arClassesInsomeClass *类型的,你投isomeClass编译器看起来像一个函数:

someClass operator[](someClass *a, someClass b) 

这样的功能不会在你的代码存在,这就是为什么你有这个错误(ERROR::"no match for 'operator[]' (operand types are 'someClass*' and 'someClass'))。

您不必投入i来访问数组元素。您之前的错误可能是由于arClassesIn[i]的类型为someClass,而new someClass(classConstructorArg)的类型为someClass *。如上所述,通过更改theFunction的原型来解决这个问题。

+0

Yesssss !!!有用!我不确定为什么它必须以这种方式工作。也许是因为它是一个指针数组而不仅仅是一个数组?我猜是这样。 无论如何,编译器报告的错误是完全无意义的,我该如何理解错误? –

+0

@ZehryoKarham:它是如何“完全无意义”? 'someClass *'和'someClass **'是不同的类型。 –

+0

我已经更新了答案以添加一些解释。在附注中,如果你不明白为什么它以这种方式工作,你应该查看一些关于指针和数组的C教程。 –

相关问题