c++(看似)随机编译错误
C++ (seemingly) Random Compiler Errors
我一直在摆弄C、c++和Allegro,这多亏了我在乐施会书店找到的一本小书和一本大书。我现在理解得很好,但是我遇到了瓶颈……每当我编译,我得到这些错误:
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
这是我的代码…
#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();
从我从调试输出中收集到的第一个错误是关于'Particle particles[max];'行,信息听起来好像在'particles'的末尾有这个'[max]'是错误的,但直到现在,它工作得很好,编译没有问题。这可能只是一个打字错误或误解或其他什么,但我真的想不出来。
正如你所看到的,这是一个粒子系统的尝试和任何关于更好的提示(这是一个词吗?)我的代码非常感谢:)
谢谢。
对于能够用作数组大小的变量,它需要是一个常量表达式。这在c++中用const
表示。在C语言中,您将使用#define
。
// 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;
现在数组的边界是一个整型常量
VLA在标准c++中是不允许的
使用:
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]
,它将工作。如果您使用#define max 50
或enum { max = 50 };
,它也可以工作。在这种情况下,enum变体可能是最好的选择。否则,您可以选择使用malloc()
动态分配粒子空间-您必须在main()
中这样做。这里的问题是int max = 0;
不是常量。如果你使用的是C语言,定义const
是没有帮助的,因为它意味着只读而不是常量。
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?