函数声明的顺序在Visual Studio C++ 2019(Unity Version)中是否重要?



我正在学习来自Java/c#/Lua/Python背景的c++。

我决定通过使用OpenGL包编写一些小游戏来学习这门语言(我还编写了一些文本解析器,并重新编写了我用c#编写的脚本语言,用c++也达到了同样的目的)。我有以下测试代码,我用来设置我的项目:

// OpenGLExample01.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
// Initializes GLUT, the display mode, and main window; registers callbacks;
// enters the main event loop.
int main(int argc, char** argv) {
// Use a single buffered window in RGB mode (as opposed to a double-buffered
// window or color-index mode).
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Position window at (80,80)-(480,380) and give it a title.
glutInitWindowPosition(80, 80);
glutInitWindowSize(400, 300);
glutCreateWindow("A Simple Triangle");
// Tell GLUT that whenever the main window needs to be repainted that it
// should call the function display().
glutDisplayFunc(display);
// Tell GLUT to start reading and processing events.  This function
// never returns; the program only exits when the user closes the main
// window or kills the process.
glutMainLoop();
}
static void display() {
// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT);
// Drawing is done by specifying a sequence of vertices.  The way these
// vertices are connected (or not connected) depends on the argument to
// glBegin.  GL_POLYGON constructs a filled polygon.
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
// Flush drawing command buffer to make drawing happen as soon as possible.
glFlush();
}

当我试图构建它时,此代码将无法编译。的原因吗?在glutDisplayFunc(display)中,"是未定义的。然而,如果我把函数翻转过来(即先定义display(),然后定义main()),程序就可以顺利构建。

#include <iostream>
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
static void display() {
// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT);
// Drawing is done by specifying a sequence of vertices.  The way these
// vertices are connected (or not connected) depends on the argument to
// glBegin.  GL_POLYGON constructs a filled polygon.
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
// Flush drawing command buffer to make drawing happen as soon as possible.
glFlush();
}
// Initializes GLUT, the display mode, and main window; registers callbacks;
// enters the main event loop.
int main(int argc, char** argv) {
// Use a single buffered window in RGB mode (as opposed to a double-buffered
// window or color-index mode).
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Position window at (80,80)-(480,380) and give it a title.
glutInitWindowPosition(80, 80);
glutInitWindowSize(400, 300);
glutCreateWindow("A Simple Triangle");
// Tell GLUT that whenever the main window needs to be repainted that it
// should call the function display().
glutDisplayFunc(display);
// Tell GLUT to start reading and processing events.  This function
// never returns; the program only exits when the user closes the main
// window or kills the process.
glutMainLoop();
}

问题是:到目前为止,我读到的所有内容都表明,第一个代码片段应该与第二个代码片段在功能上相同。这是怎么回事?

是的,为了使用它,编译器需要看到声明。

只需要声明,不需要定义。

static void bar(); // declaration
void foo(){
bar();
}
static void bar(){ // function body here (definition)
/*do something*/
};

最新更新