类中向量的问题
时间:2020-03-06 14:52:59 来源:igfitidea点击:
我在一个类中有以下代码:
void SendStones() { int currenthole = hole; int lastplace = 0; for(int i=0;i<stns.size();i++) { while(1) {//Calculate new currenthole if(currenthole == 13) { currenthole = 7; break;} if(currenthole == 14) { currenthole = 6; break;} if((currenthole<=12 && currenthole > 7) || (currenthole<=6 && currenthole > 1)) { currenthole--; break;} } lastplace = stns.size()-1; hole[currenthole]->ReciveStone(stns[lastplace]);//PROBLEM stns.pop_back(); } } vector<Stones*> stns;
所以会出现这个错误:
数组下标的无效类型'int [int]'
有什么问题?我听不懂。
谢谢。
解决方案
看来Hole是一个简单的int,并且我们要尝试对其进行下标。那是你的意思吗?孔在哪里宣布?
霍尔是非常重要的一类,
SendStones是该类中的一个函数成员。
我不会发送整个文件,但是我可以说
hole [currenthole]是一个Hole * hole [14];
这是一个很大的程序和项目,所以我发送了所需的相关代码。
这是ReciveStones函数的代码:
无效ReciveStone(Stone * rcvstone)
{
stns.push_back(rcvstone);
}
根据我们在回答中说的内容,hole是指向n个Hole对象的指针。这意味着代码没有按照我们认为的那样做。
int currenthole = hole;
这是存储指向数组集合中第一个对象的地址值,这意味着此代码
if(currenthole == 13) { currenthole = 7; break;} if(currenthole == 14) { currenthole = 6; break;} if((currenthole<=12 && currenthole > 7) || (currenthole<=6 && currenthole > 1)) { currenthole--; break;}
可能是胡说八道。
它没有解释为什么我们会收到"数组下标无效的类型'int [int]'"错误。我们确定没有第二个类型为int的int声明?
-实际上,重新阅读我们写的内容后,我什至可以确定我们没有按照自己的想法去做。 SendStones是Hole类的成员,对吗?检查Hole类中是否没有Hole成员变量。这可能是问题所在,因为它将在任何称为Hole的全局变量之前找到(如果我正确记住了作用域规则)。