c++ - 数组下标错误的类型 'int[int]'无效

我正从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++;
        }
    }  
}

最佳答案

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

testApp.h:

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

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

https://stackoverflow.com/questions/15100581/

相关文章:

Excel VBA 错误 - 函数或接口(interface)标记为受限。或函数使用 Visual

iphone - NSInvalidArgumentException与UITableView吗?

java - Java : Longest Ascending substring

compilation - 安装MPFR(3.1.0)时出错

c++ - 编译器错误 : Undefined symbols for architecture x

ios - iOS静态库取决于其他静态库

c++ - OpenCV无法编译

java - 升序子字符串编译错误

apache - htaccess 将所有页面重定向到单个页面

ruby-on-rails - Rails ActionView::Template::Error: