C++;(看似)随机编译器错误 我一直在玩C、C++和AlReGo,这多亏了我在牛津饥荒救济委员会书店发现的一本书和一本更大的书。 我现在很理解,但我遇到了麻烦。。。无论何时编译,都会出现以下错误: archiboldian@archiboldian:~/Documents/C++ Projects/particles$ g++ particles.c -lalleg -lnoise -o particles particles.c:19: error: array bound is not an integer constant before ‘]’ token particles.c:20: error: ‘Vector2D’ does not name a type particles.c:21: error: ‘Vector2D’ does not name a type particles.c: In function ‘int main()’: particles.c:26: error: ‘nPos’ was not declared in this scope particles.c:28: error: ‘nVel’ was not declared in this scope particles.c:29: error: ‘nvel’ was not declared in this scope particles.c:31: error: ‘addParticle’ was not declared in this scope particles.c: At global scope: particles.c:47: error: ‘Vector2D’ has not been declared particles.c:47: error: ‘Color’ has not been declared particles.c: In function ‘void addParticle(int, int, Vector2d, int, int, int)’: particles.c:50: error: ‘particles’ was not declared in this scope
这是我的密码C++;(看似)随机编译器错误 我一直在玩C、C++和AlReGo,这多亏了我在牛津饥荒救济委员会书店发现的一本书和一本更大的书。 我现在很理解,但我遇到了麻烦。。。无论何时编译,都会出现以下错误: archiboldian@archiboldian:~/Documents/C++ Projects/particles$ g++ particles.c -lalleg -lnoise -o particles particles.c:19: error: array bound is not an integer constant before ‘]’ token particles.c:20: error: ‘Vector2D’ does not name a type particles.c:21: error: ‘Vector2D’ does not name a type particles.c: In function ‘int main()’: particles.c:26: error: ‘nPos’ was not declared in this scope particles.c:28: error: ‘nVel’ was not declared in this scope particles.c:29: error: ‘nvel’ was not declared in this scope particles.c:31: error: ‘addParticle’ was not declared in this scope particles.c: At global scope: particles.c:47: error: ‘Vector2D’ has not been declared particles.c:47: error: ‘Color’ has not been declared particles.c: In function ‘void addParticle(int, int, Vector2d, int, int, int)’: particles.c:50: error: ‘particles’ was not declared in this scope,c++,c,compiler-construction,compiler-errors,allegro,C++,C,Compiler Construction,Compiler Errors,Allegro,这是我的密码 #include "allegro.h" struct Vector2d{ double x; double y; }; struct Particle { Vector2d Pos; Vector2d Vel; int age; int LifeSpan; int colour; int size; }; int max = 50; int pcount = 0; Particle particles[max]
#include "allegro.h"
struct Vector2d{
double x;
double y;
};
struct Particle {
Vector2d Pos;
Vector2d Vel;
int age;
int LifeSpan;
int colour;
int size;
};
int max = 50;
int pcount = 0;
Particle particles[max];
int main(void) {
Vector2D nPos;
Vector2D nVel;
nPos.x = 320;
nPos.y = 240;
nVel.x = 2;
nvel.y = 0;
addParticle(10, nPos, nVel, 20, makecol(255,255,255), 2);
allegro_init();
install_keyboard();
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
while(!key[KEY_ESC]) {
for(int i=0;i<pcount;i++){
}
}
allegro_exit();
}
void addParticle(int addp, Vector2D Pos, Vector2d Vel, int LifeSpan, Color colour, int size) {
for(int i=0;i<addp;i++){
pcount++;
particles[pcount].Pos = Pos;
particles[pcount].Vel = Vel;
particles[pcount].LifeSpan = LifeSpan;
particles[pcount].colour = colour;
particles[pcount].size = size;
}
}
END_OF_MAIN();
#包括“allegro.h”
结构向量2D{
双x;
双y;
};
结构粒子{
矢量2D位置;
向量2D层;
智力年龄;
寿命;
内部色彩;
整数大小;
};
int max=50;
int pcount=0;
粒子[max];
内部主(空){
向量2D NPO;
向量向量;
nPos.x=320;
nPos.y=240;
nVel.x=2;
nvel.y=0;
addParticle(10,nPos,nVel,20,makecol(255255),2);
第一快板();
安装键盘();
设置gfx模式(gfx自动检测窗口,640480,0,0);
而(!key[key\u ESC]){
对于(int i=0;i<p),一个变量可以用作数组大小,它需要是一个常量表达式。这是用C++中的代码> const 来表示的。
// C++
const int MAX = 50;
/* C */
#define MAX 50
/* both C & C++ */
enum { MAX = 50 };
Particle particles[MAX];
该错误解释了问题:
particles.c:19: error: array bound is not an integer constant before ‘]’ token
修复方法:
const int max = 50;
现在数组绑定是一个整数常量。标准C++中不允许
< P> VLA。< /P>
使用以下命令:
const int max = 50;
因为数组大小必须是一个常量表达式。如果没有const
关键字,max
不是常量表达式。更改为const int max=50;
这是一个编译时常量,因此可以使用它初始化数组。(注意,并非所有const
变量都是编译时常量)
也将文件重命名为<代码> *.CPP,因此GCC将理解C++的构造。它似乎是编译为“代码> C>代码>语言代码。
Joachim Pileborg观察到,您同时拥有Vector2d
和Vector2d
我怀疑这段代码是否能够编译
Vector2D
不是正确的类型。struct Vector2D
是正确的类型。(因此会出现许多错误)。您可以使用typedef struct Vector2D Vector2D
获得预期的行为。不确定Vector2D
(小写d
)的情况
- 无法动态定义全局表变量的空间。如果执行
particles[50]
,它将起作用。如果定义max 50
或enum{max=50},它也将起作用;
。在这种情况下,枚举变量可能是您的最佳选择。否则,您可以选择使用malloc()
-为粒子动态分配空间-您必须在main()中执行此操作
。这里的问题是int max=0;
不是常量。如果使用C,定义它const
将没有帮助,因为它意味着只读而不是常量
尝试将max声明为
const int max=50
Vector2D
或Vector2D
…选择一个…;)问题是C++和C的标记,但是问题是C++的,而文件名表示C++和C是两种不同的语言。你也把文件编译成C++。选择一个…谢谢解释错误和问题之间的联系。关于代码的第一个要点是:VCARD2D</代码> VS <代码>结构向量2D/COD>应用到C++中。第二点也是C特定的。当问题中使用的文件名为C时,问题标题为C++,文件被编译成C++。嘿,很抱歉。我浏览了代码,没有看到任何C++特定的东西,所以我只是假设它是C。但是我感兴趣——如果我得到了这个。对,C++中,结构是自动的类型?与其说它们是神奇的TyPufFor,不如说C++ C++允许C++引用没有Strut关键字的结构。也许它是迂腐的,但是在一个结构的定义中,你可以使用Strut的未修饰名称,而对于Type则不能这样做:<代码> C++:o{Foo*f;};所以我假设在C语言中,像typedef'd类型“a”、类型“union a”和类型“struct a”这样的东西是非常糟糕的做法它们都是不同的吗?或者有这样的情况吗?我正在为假设的SIC处理器编写一个小型虚拟机,并使用位域结构的并集来解析指令,其中并集与原始“指令”具有相同的名称我相信C++中使用一个结构和一个并集的标签是不合法的,尽管我没有引用它。无论如何,我的C编译器拒绝了。对于一个结构阴影,TyBufff的结构名称在C和C++中都有作用。C++中的N个错误。我可能会避免使用不同的名称,但是如果你不希望程序需要被构建成C++,那就没关系了。我不会说它是一个非常糟糕的练习。在这两种语言中你也可以使用<代码> EnUM < /Cord>常数。Jens:我知道我忘了什么。谢谢。