错误:“应用”未在此范围内声明

error: ‘app’ was not declared in this scope

本文关键字:范围内 声明 应用 错误      更新时间:2023-10-16

我正在尝试在 Ubuntu 中运行超级圣经版本 7 中的示例。我已经在 Windows 中成功完成了此操作,但是,使用 Ubuntu,它会抛出此错误

sb7.h:346:8: error: ‘app’ was not declared in this scope
     a *app = new a;                                 
        ^
main.cpp:18:1: note: in expansion of macro ‘DECLARE_MAIN’
DECLARE_MAIN(my_application());
^

main.cpp

#ifndef _LINUX
#define _LINUX
#endif
#include "sb7.h"
class my_application : public sb7::application
{
public:
    void render(double currentTime)
    {
        static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
        glClearBufferfv(GL_COLOR, 0, red);
    }
};
DECLARE_MAIN(my_application());

sb7.h

#ifndef __SB7_H__
#define __SB7_H__
#ifdef WIN32
    #pragma once
    #define _CRT_SECURE_NO_WARNINGS 1
    #define WIN32_LEAN_AND_MEAN 1
    #include <Windows.h>
#else
    #include <unistd.h>
    #define Sleep(t) sleep(t)
#endif
#include "GL/gl3w.h"
#define GLFW_NO_GLU 1
#define GLFW_INCLUDE_GLCOREARB 1
#include "GLFW/glfw3.h"
#include "sb7ext.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
namespace sb7
{
class application
{
private:
    static void APIENTRY debug_callback(GLenum source,
                                        GLenum type,
                                        GLuint id,
                                        GLenum severity,
                                        GLsizei length,
                                        const GLchar* message,
                                        GLvoid* userParam);
public:
    application() {}
    virtual ~application() {}
    virtual void run(sb7::application* the_app)
    {
        bool running = true;
        app = the_app;
        if (!glfwInit())
        {
            fprintf(stderr, "Failed to initialize GLFWn");
            return;
        }
        init();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, info.majorVersion);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, info.minorVersion);
#ifndef _DEBUG
        if (info.flags.debug)
#endif /* _DEBUG */
        {
            glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
        }
        if (info.flags.robust)
        {
            glfwWindowHint(GLFW_CONTEXT_ROBUSTNESS, GLFW_LOSE_CONTEXT_ON_RESET);
        }
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint(GLFW_SAMPLES, info.samples);
        glfwWindowHint(GLFW_STEREO, info.flags.stereo ? GL_TRUE : GL_FALSE);
//        if (info.flags.fullscreen)
//        {
//            if (info.windowWidth == 0 || info.windowHeight == 0)
//            {
//                GLFWvidmode mode;
//                glfwGetDesktopMode(&mode);
//                info.windowWidth = mode.Width;
//                info.windowHeight = mode.Height;
//            }
//
//            glfwOpenWindow(info.windowWidth, info.windowHeight, 8, 8, 8, 0, 32, 0, GLFW_FULLSCREEN);
//            glfwSwapInterval((int)info.flags.vsync);
//        }
//        else
        {
            window = glfwCreateWindow(info.windowWidth, info.windowHeight, info.title, info.flags.fullscreen ? glfwGetPrimaryMonitor() : NULL, NULL);
            if (!window)
            {
                fprintf(stderr, "Failed to open windown");
                return;
            }
        }
        glfwMakeContextCurrent(window);
        glfwSetWindowSizeCallback(window, glfw_onResize);
        glfwSetKeyCallback(window, glfw_onKey);
        glfwSetMouseButtonCallback(window, glfw_onMouseButton);
        glfwSetCursorPosCallback(window, glfw_onMouseMove);
        glfwSetScrollCallback(window, glfw_onMouseWheel);
        if (!info.flags.cursor)
        {
            glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
        }
        // info.flags.stereo = (glfwGetWindowParam(GLFW_STEREO) ? 1 : 0);
        gl3wInit();
#ifdef _DEBUG
        fprintf(stderr, "VENDOR: %sn", (char *)glGetString(GL_VENDOR));
        fprintf(stderr, "VERSION: %sn", (char *)glGetString(GL_VERSION));
        fprintf(stderr, "RENDERER: %sn", (char *)glGetString(GL_RENDERER));
#endif
        if (info.flags.debug)
        {
            if (gl3wIsSupported(4, 3))
            {
                glDebugMessageCallback((GLDEBUGPROC)debug_callback, this);
                glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
            }
            else if (sb6IsExtensionSupported("GL_ARB_debug_output"))
            {
                glDebugMessageCallbackARB((GLDEBUGPROC)debug_callback, this);
                glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
            }
        }
        startup();
        do
        {
            render(glfwGetTime());
            glfwSwapBuffers(window);
            glfwPollEvents();
            running &= (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_RELEASE);
            running &= (glfwWindowShouldClose(window) != GL_TRUE);
        } while (running);
        shutdown();
        glfwDestroyWindow(window);
        glfwTerminate();
    }
    virtual void init()
    {
        strcpy(info.title, "OpenGL SuperBible Example");
        info.windowWidth = 800;
        info.windowHeight = 600;
#ifdef __APPLE__
        info.majorVersion = 3;
        info.minorVersion = 2;
#else
        info.majorVersion = 4;
        info.minorVersion = 3;
#endif
        info.samples = 0;
        info.flags.all = 0;
        info.flags.cursor = 1;
#ifdef _DEBUG
        info.flags.debug = 1;
#endif
    }
    virtual void startup()
    {
    }
    virtual void render(double currentTime)
    {
    }
    virtual void shutdown()
    {
    }
    void setWindowTitle(const char * title)
    {
        glfwSetWindowTitle(window, title);
    }
    virtual void onResize(int w, int h)
    {
        info.windowWidth = w;
        info.windowHeight = h;
    }
    virtual void onKey(int key, int action)
    {
    }
    virtual void onMouseButton(int button, int action)
    {
    }
    virtual void onMouseMove(int x, int y)
    {
    }
    virtual void onMouseWheel(int pos)
    {
    }
    virtual void onDebugMessage(GLenum source,
                                GLenum type,
                                GLuint id,
                                GLenum severity,
                                GLsizei length,
                                const GLchar* message)
    {
#ifdef _WIN32
        OutputDebugStringA(message);
        OutputDebugStringA("n");
#endif /* _WIN32 */
    }
    void getMousePosition(int& x, int& y)
    {
        double dx, dy;
        glfwGetCursorPos(window, &dx, &dy);
        x = static_cast<int>(floor(dx));
        y = static_cast<int>(floor(dy));
    }
public:
    struct APPINFO
    {
        char title[128];
        int windowWidth;
        int windowHeight;
        int majorVersion;
        int minorVersion;
        int samples;
        union
        {
            struct
            {
                unsigned int    fullscreen  : 1;
                unsigned int    vsync       : 1;
                unsigned int    cursor      : 1;
                unsigned int    stereo      : 1;
                unsigned int    debug       : 1;
                unsigned int    robust      : 1;
            };
            unsigned int        all;
        } flags;
    };
protected:
    APPINFO     info;
    static      sb7::application * app;
    GLFWwindow* window;
    static void glfw_onResize(GLFWwindow* window, int w, int h)
    {
        app->onResize(w, h);
    }
    static void glfw_onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
    {
        app->onKey(key, action);
    }
    static void glfw_onMouseButton(GLFWwindow* window, int button, int action, int mods)
    {
        app->onMouseButton(button, action);
    }
    static void glfw_onMouseMove(GLFWwindow* window, double x, double y)
    {
        app->onMouseMove(static_cast<int>(x), static_cast<int>(y));
    }
    static void glfw_onMouseWheel(GLFWwindow* window, double xoffset, double yoffset)
    {
        app->onMouseWheel(static_cast<int>(yoffset));
    }
    void setVsync(bool enable)
    {
        info.flags.vsync = enable ? 1 : 0;
        glfwSwapInterval((int)info.flags.vsync);
    }
};
};
#if defined _WIN32
#define DECLARE_MAIN(a)                             
sb7::application *app = 0;                          
int CALLBACK WinMain(HINSTANCE hInstance,           
                     HINSTANCE hPrevInstance,       
                     LPSTR lpCmdLine,               
                     int nCmdShow)                  
{                                                   
    a *app = new a;                                 
    app->run(app);                                  
    delete app;                                     
    return 0;                                       
}
#elif defined _LINUX || defined __APPLE__
#define DECLARE_MAIN(a)                             
int main(int argc, const char ** argv)              
{                                                   
    a *app = new a;                                 
    app->run(app);                                  
    delete app;                                     
    return 0;                                       
}
#else
#error Undefined platform!
#endif
#endif /* __SB7_H__ */

生成文件是

CXX        = g++
CXXFLAGS   = -Wall -W -std=c++11
LD         = g++

INCLUDEDIR = /home/XXX/CPP_Projects/ModernOpenGL/sb7code-master/include
LIBDIR     = /home/XXX/CPP_Projects/ModernOpenGL/sb7code-master/build/lib
LIBFILE    = -lsb7 -lGLEW -lglfw3 -lGL -ldl -lX11 -lXi -lXrandr -lXxf86vm -lXinerama -lXcursor -lrt -lm -lpthread

INCLUDES   = -I$(INCLUDEDIR)
LIBS       = -L$(LIBDIR) $(LIBFILE)
EXEEXT     = .exe
TARGET     = test
EXECUTABLE = $(TARGET)$(EXEEXT)
OBJS       = $(TARGET).o
$(EXECUTABLE): $(OBJS)
    $(LD) $(OBJS) $(LIBS) -o $(TARGET)
.cpp.o:
    $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
.PHONY: clean
clean:
    rm -f $(OBJS) $(TARGET)$(EXEEXT)

这个:

DECLARE_MAIN(my_application());

大概应该是这个:

DECLARE_MAIN(my_application);

因为my_application是一个类,而您的宏希望您传递类的名称。

旁注:所有这些,都是远离宏的原因之一。