简体   繁体   English

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

[英]Does the order of a function's declaration matter in Visual Studio C++ 2019 (Unity Version)?

I am learning C++ coming from a Java/C#/Lua/Python background.我正在学习来自 Java/C#/Lua/Python 背景的 C++。

I decided to learn the language by writing some small games using the OpenGL package (I'm also writing some text parsers and re-writing a scripting language I wrote in C#, in C++ to the same ends).我决定通过使用 OpenGL package 编写一些小游戏来学习这门语言(我也在编写一些文本解析器并重写我在 C# 和 C++ 中编写的脚本语言以达到相同的目的)。 I have the following test code I am using to set up my project:我有以下用于设置项目的测试代码:

// 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();
}

This code won't compile when I try to build it.当我尝试构建此代码时,它不会编译。 The reason?原因? In glutDisplayFunc(display), "display" is undefined.在 glutDisplayFunc(display) 中,“display”是未定义的。 However, if I flip the functions around (ie define display() first, then define main()), the program builds without a hitch.但是,如果我翻转函数(即先定义 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();
}

Here's the issue: Everything I've read thus far suggests that the first code snippet should be functionally the same as the second snippet.问题是:到目前为止我读到的所有内容都表明第一个代码片段在功能上应该与第二个代码片段相同。 What's going on here?这里发生了什么?

yes, in order to use it, the compiler need to see the declaration.是的,为了使用它,编译器需要查看声明。

Only the declaration is needed though, not necessary the definition.但是只需要声明,不需要定义。

static void bar(); // declaration

void foo(){
    bar();
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM