c ++,当部分代码放在另一个cpp文件中时,标识符是未定义的

时间:2015-07-08 11:00:02

标签: c++ undefined

对不起,如果问题布局很奇怪,或者这是我第一次提出问题。我一周前开始学习c ++,现在我知道如何使用GLFW创建一个窗口我想清理我的代码并使其更好。我试着把#34;创建窗口"名为" window.cpp"的.cpp文件中的内容然后将其导入主文件。但是当我拿出GLFWCreateWindow函数时,它不会识别窗口名称" w_gill"在swapbuffer中,windowshouldclose和destroywindow函数。有人可以帮帮我吗?

这是Main.cpp文件:

#include <iostream>
#include <Windows.h>
#include "window.cpp"

int main(){
    do{
        createWindow();
        glfwSwapBuffers(w_gill);
        glfwPollEvents();
    } while (!glfwWindowShouldClose(w_gill));

    glfwDestroyWindow(w_gill);
    glfwTerminate();
    return 0;
}

Window.cpp文件:

#include <GL\glew.h>
#include <GLFW\glfw3.h>

int windowWidth = 1920 / 2;
int windowHeight = 1080 / 2;

int createWindow(){
    if (!glfwInit())
    {
        glfwTerminate();
        return -1;
    }
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_VERSION_MINOR, 4);
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    GLFWwindow* w_gill;
    w_gill = glfwCreateWindow(windowWidth, windowHeight, "Gillow", NULL, NULL);

    if (!w_gill)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(w_gill);
    glfwSetInputMode(w_gill, GLFW_STICKY_KEYS, GL_TRUE);
    }

2 个答案:

答案 0 :(得分:1)

没关系,我得到了它,我不得不放

sub(".*(?<=\\()(.+)(?=\\)).*", "\\1", "A B C (123-456-789)", perl=TRUE)
#[1] "123-456-789"

在main.cpp文件中,所以它们是链接的。无论如何,谢谢你的回复。

答案 1 :(得分:0)

您的问题与变量在scope中的“C”或生命周期有关。 w_gill的范围是其包含函数的局部范围,因此,该函数之外的变量没有可见性。除了将createWindow()函数移动到其自己的.CPP文件之外,您还必须进行一些其他更改,即使它仍在main.cpp中,您仍然会遇到相同的问题。