C ++编译器在一个源文件中的一个函数调用中引发错误,但在具有相同函数调用的另一个源文件中则未引发错误

时间:2019-04-18 11:52:17

标签: c++

我有两个C ++源文件,其代码中包含以下代码段。

...
unsigned int triangleVaos[] = { 0, 0 };
unsigned int triangleVbos[] = { 0, 0 };
unsigned int numTriangleVaos = sizeof(triangleVaos) / sizeof(*triangleVaos);
unsigned int numTriangleVbos = sizeof(triangleVbos) / sizeof(*triangleVbos);
for (int index = 0; index < numTriangleVaos; index++) {
    unsigned int* triangleVao = &triangleVaos[index];
    glGenVertexArrays(1, triangleVaos);

    glBindVertexArray(triangleVao);
    ...
...

我正在使用以下命令编译源文件

g++ -g \
    -std=c++11 \
    -I include/ \
    -o src/1-getting-started/exer-hello-triangle-3 \
    src/1-getting-started/exer-hello-triangle-3.cpp src/utils/glad.c \
    -lglfw -lGL -lX11 -lpthread -lXrandr -lXi -ldl;

当我编译第一个源文件时,一切正常。但是,无论何时编译第二个源文件,都会收到以下错误消息:

src/1-getting-started/exer-hello-triangle-3.cpp: In function ‘int main()’:
src/1-getting-started/exer-hello-triangle-3.cpp:156:38: error: invalid conversion from ‘unsigned int*’ to ‘GLuint {aka unsigned int}’ [-fpermissive]
     glBindVertexArray(triangleVao);

这迫使我将第二个源文件中的代码段修改为:

...
for (int index = 0; index < numTriangleVaos; index++) {
    unsigned int* triangleVao = &triangleVaos[index];
    glGenVertexArrays(1, triangleVaos);

    glBindVertexArray(*triangleVao);
...

现在可以成功编译第二个源文件。

为什么会这样?

1 个答案:

答案 0 :(得分:-1)

(此答案存在,以防有人偶然发现此问题。)

事实证明我错误地设置了编译命令(我正在使用 request .post('/upload') .field('user[name]', 'Tobi') .field('user[email]', 'tobi@learnboost.com') .field('friends[]', ['loki', 'jane']) .attach('image', 'path/to/tobi.png') .then(callback); )。将第一个源文件的编译命令设置为编译其他(但无错误)的源文件,这将导致编译成功。正确地将第二个源文件的编译命令设置为编译第二个源文件,该文件有错误,因此无法编译。

此处的课程是确保正确设置编译命令以编译要编译的文件。一个被忽略的简单错误可能会在以后引起巨大的问题。