数组下标错误'int[int]'的类型无效
Invalid types 'int[int]' for array subscript error
我正在从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]
),然后您应该能够在draw()
函数中引用数组而不会出现错误。
testApp.h:
private:
int* hueValues;
int* saturationValues;
int* brightnessValues;
testApp.cpp
void testApp::setup(){
hueValues = new int[colorCount];
...
}
相关文章:
- 为什么 int 类型的枚举类值不能用作 int
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 比较两个整数在C++中与未知 int 类型的相等性
- 二进制'operator+' 'int*'和'int*'类型的无效操作数
- 使用元编程选择 int 类型,将生成错误
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 复制初始化 - 从 'int' 类型转换为非标量类型
- 初始化cv :: mat ::具有int类型的定义大小的人
- 处理代码重新定义int类型(uint16_t,int16_t等),并不喜欢它
- 为什么没有"int"类型声明时输出不同?
- 在大型程序中将长变量更改为 int 类型
- 无法将 int 类型转换为时间类型(我的类类型)
- C++:无法在按位函数后将 int 类型设置为非标量
- 无法分配给INT类型的成员[10]
- C++ cin 将字符串读取到 int 类型中返回 0
- 为什么 decltype((i)) 是引用类型,而 decltype(i+0) 是 int 类型
- 使用4用于循环的4矩阵上的数组下标的“ int [int]类型”
- std::is_arithmetic 为通用 lambda 中的 int 类型返回 false:未定义的行为?
- 正在施放带有“ Int”类型的临时性