【问题标题】:Invalid types 'int[int]' for array subscript error数组下标错误的无效类型“int [int]”
【发布时间】:2013-02-12 13:56:11
【问题描述】:

我正在从 Java 世界迁移到 C++,并尝试移植一个示例。我收到一个错误,因为我没有正确访问我的数组。我尝试使用 -> 和 * 符号指向它们,但我很难理解它。我相信这是多次重复的相同基本错误。我已经强调了错误开始的三个区域,不包括相同错误的倍数。任何帮助都会对我理解这个话题大有帮助。谢谢!

//--------------------------------------------------------------
void testApp::setup(){
    colorCount = 20;
    int *hueValues = new int[colorCount];
    int *saturationValues = new int[colorCount];
    int *brightnessValues = new int[colorCount];

ofColor::fromHsb(360, 100, 100, 100);
ofFill();
}

//--------------------------------------------------------------
void testApp::update(){

}

//--------------------------------------------------------------
void testApp::draw(){
    // ------ colors ------
    // create palette
    for (int i=0; i<colorCount; i++) {
        if (i%2 == 0) {
            hueValues[i] = (int) random(0,360);                  // invalid types 'int[int]' fr array subscript
            saturationValues[i] = 100;  
            brightnessValues[i] = (int) random(0,100);
        } 
        else {
            hueValues[i] = 195;
            saturationValues[i] = (int) random(0,100);
            brightnessValues[i] = 100;
        }
    }

    // ------ area tiling ------
    // count tiles
    int counter = 0;
    // row count and row height
    int rowCount = (int)random(5,40);                  // At this point in file
    float rowHeight = (float)ofGetHeight()/(float)rowCount;

    for(int i=0; i<rowCount; i++) {
        // seperate each line in parts  
        // how many fragments
        int partCount = i+1;
        parts = new float[0];                  // Cannot convert "float" to "float" in assignment

        for(int ii=0; ii<partCount; ii++) {
            // sub fragments or not?
            if (random(1.0) < 0.075) {
                // take care of big values      
                int fragments = (int)random(2,20);
                partCount = partCount + fragments; 
                for(int iii=0; iii<fragments; iii++) {
                    parts = append(parts, random(2));
                }              
            }  
            else {
                parts = append(parts, random(2,20));   
            }
        }

        // add all subparts
        float sumPartsTotal = 0;
        for(int ii=0; ii<partCount; ii++) sumPartsTotal += parts[ii];

        // draw rects
        float sumPartsNow = 0;
        for(int ii=0; ii<parts.length; ii++) {
            // get component color values
            int index = counter % colorCount;
            fill(hueValues[index],saturationValues[index],brightnessValues[index]);

            sumPartsNow += parts[ii];
            rect(map(sumPartsNow, 0,sumPartsTotal, 0,width),rowHeight*i, 
                 map(parts[ii], 0,sumPartsTotal, 0,width)*-1,rowHeight);

            counter++;
        }
    }  
}

【问题讨论】:

  • int *hueValues = new int[colorCount]; - 这声明了 testApp::setup 函数的局部变量。也许你不小心将你的成员变量声明为int
  • 请停止使用new[]并切换到像std::vector这样的标准容器。
  • 好吧,就像我说的,我正在从 Java 迁移到 C++。 new[] 是 Java 的标准符号。我会试试看是否有帮助。
  • 对动态签名数组使用std::vector。指向动态分配对象的原始指针通常会带来麻烦。
  • 好的,我想采用最佳编码实践。如何在我的代码中实现std::vector

标签: c++ arrays compiler-errors subscript


【解决方案1】:

看起来您正在为 setup() 函数中的数组分配空间,但您的指针是在本地定义的,因此一旦函数完成执行,指针就会超出范围;当您尝试在 draw() 函数中引用它们时,您会收到编译器错误,因为未找到/未正确定义符号。将int *hueValues/saturationValues/brightnessValues 声明移动到testAPP 头文件的私有部分,在setup 函数中保留分配代码(new int[coloCount]),然后您应该能够在不使用draw() 函数的情况下引用数组错误。

testApp.h:

private:
int* hueValues;
int* saturationValues;
int* brightnessValues;

testApp.cpp

void testApp::setup(){
  hueValues = new int[colorCount];
  ...
}

【讨论】:

  • 这似乎从我的 setup() 函数中删除了错误,但现在 draw() 中的错误只是说:“此时在文件中”并且没有给我太多调试信息。每当我尝试为已初始化的数组分配随机值时,它们似乎都会系统地出现。例如,在:hueValues[i] = (int) random(0,360);
  • 随机定义在哪里?能否提供完整的编译错误?
猜你喜欢
  • 2015-09-26
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 2010-09-26
  • 2012-09-07
相关资源
最近更新 更多