【问题标题】:How do i fix this specific "undeclared identifier" error?如何修复这个特定的“未声明的标识符”错误?
【发布时间】:2022-01-17 05:27:10
【问题描述】:

我正在尝试学习如何使用glad 和glfw 在c 中制作游戏和模拟。尝试在任何函数中将结构 Window 用作参数或仅声明 Window 实例时发生此错误。我收到'Window': undeclared identifier 错误。我知道这可能意味着通过研究 stackoverflow 上的错误,我有一个循环包含(我似乎无法弄清楚在哪里)。 (我对 c 很陌生,所以我很乐意提供任何帮助)


Core.h:

#ifndef MINECRAFTCLONE_CORE_H
#define MINECRAFTCLONE_CORE_H

#include <stdio.h>

#include <glad/glad.h>
#include <GLFW/glfw3.h>

extern int error(char* error);

#endif

Core.c:

#include "Core.h"

int error(char* error)
{
    printf("%s", error);
    return -1;
}

Window.h:

#ifndef CORE_WINDOW_H
#define CORE_WINDOW_H

#include "Core.h"

struct Window
{
    int width;
    int height;
    char* title;
    GLFWwindow* res;
};

extern int coreCreateWindow(struct Window* window,
                            int width, int height, char* title);
extern int coreLoopWindow(struct Window* window);

#endif

Window.c:

#include "Core.h"
#include "Window.h"

int coreCreateWindow(struct Window* window,
                     int width, int height, char* title)
{
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    if (!glfwInit())
        return error((char*)"Failed to initialize glfw");

    window->width = width;
    window->height = height;
    window->title = title;
    window->res = glfwCreateWindow(window->width, window->height,
                                   window->title, 0, 0);

    if (!window->res)
        return error((char*)"Failed to create glfw window");

    glfwMakeContextCurrent(window->res);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        return error((char*)"Failed to initialize glad");

    return 0;
}

int coreLoopWindow(struct Window* window)
{
    while (!glfwWindowShouldClose(window->res))
    {
        glfwPollEvents();
    }

    glfwDestroyWindow(window->res);
    glfwTerminate();

    return 0;
}

ma​​in.c:

#include "Core.h"
#include "Window.h"


int main()
{
    Window* window;

    return 0;
}

【问题讨论】:

    标签: c glfw glad


    【解决方案1】:

    还没有定义一个名为 Window 的类型,您可以使用它定义一个变量,如

    Window *window;
    

    已经定义了一个struct Window,您可以使用它在main() 中定义您的window 变量,例如

    struct Window *window;
    

    以同样的方式定义所有函数原型的window 参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2013-01-22
      • 2011-10-25
      • 2011-02-15
      • 1970-01-01
      相关资源
      最近更新 更多