我正在一个名为"initialize.cpp";并在";发动机.h";。当我尝试在Main中使用该类时,它会为所使用的每个函数提供一个链接器错误。
unresolved external symbol "public: int __cdecl Application::run(void)" (?run@Application@@QEAAHXZ) referenced in function main
unresolved external symbol "public: int __cdecl Application::loop(void)" (?loop@Application@@QEAAHXZ) referenced in function main
unresolved external symbol "public: int __cdecl Application::termination(void)" (?termination@Application@@QEAAHXZ) referenced in function main
这是initialize.cpp
#include<GL/glew.h>
#include <GLFW/glfw3.h>
class Application {
public:
int run() {
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(1600, 900, "OpenGL Window", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
return 0;
}
void loop() {
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
}
int termination() {
glfwTerminate();
return 0;
}
private:
GLFWwindow* window;
};
这是头文件
#pragma once
class Application {
public:
int run();
int loop();
int termination();
private:
};
和主
#include "Engine/Engine.h"
int main()
{
Application application;
application.run();
application.loop();
application.termination();
return 0;
}
请记住,我对C++还很陌生,如果我忽略了一些愚蠢的东西,我很抱歉,但总是愚蠢的错误让我们学习。
这里是initialize.cpp
#include<GL/glew.h> #include <GLFW/glfw3.h> class Application { public: int run() { /* Initialize the library */ if (!glfwInit()) return -1; //...
这不是实现类的成员函数的方式。这样做的目的是定义一个完全不同的class Application
,其所有成员函数都是内联的,其他.cpp
文件无法访问,而main.cpp
只看到在标头中声明的class Application
,其成员函数没有实现。
假设在标头initialize.h
中声明了class Application
,则改为按如下方式更改initialize.cpp
。
#include<GL/glew.h>
#include <GLFW/glfw3.h>
#include "initialize.h"
int Application::run() {
/* Initialize the library */
if (!glfwInit())
return -1;
//...
return 0;
}
void Application::loop() {
//...
}
int Application::termination() {
//...
}