使用其他 class 的构造函数时出现链接器错误 "unresolved external symbol"

Linker Error "unresolved external symbol" when using constructor of other class

我一直在努力寻找为什么我的链接器会出现无法解析的外部符号错误。错误如下所示:

Error
LNK2019
unresolved external symbol "public: __thiscall Shader::Shader(char const *)" (??0Shader@@QAE@PBD@Z) referenced in function "public: __thiscall GridWorldGPGPU::GridWorldGPGPU(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,unsigned int)" (??0GridWorldGPGPU@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z)
DV2556_Project  
grid_world_GPGPU.obj
1       

据我了解,这与我的链接器找到 Shader::Shader(char const *) 函数的声明但找不到定义有关。我已经盯着这个看了好几个小时了,想不通为什么链接器会变得不高兴。

grid_world_GPGPU.h:

#ifndef GRID_WORLD_GPGPU_H
#define GRID_WORLD_GPGPU_H

#include "grid_world.h"
#include <../swift_open_gl.h>

class GridWorldGPGPU : public GridWorld {
private:
    Shader* compute_shader_ = nullptr;

public:
    GridWorldGPGPU(std::string in_path_shader, unsigned int in_side = 1);
};

#endif // !GRID_WORLD_GPGPU_H

grid_world_GPGPU.cpp:

GridWorldGPGPU::GridWorldGPGPU(std::string in_path_shader, unsigned int in_side)
    : GridWorld(in_side) {
    this->compute_shader_ = new Shader(in_path_shader.c_str());
}

Shader-class定义在swift_open_gl.h文件中:

#ifndef SWIFT_OPEN_GL_H
#define SWIFT_OPEN_GL_H

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

class Shader {
public:
    Shader(const char* cs_path);
};
#endif // !SWIFT_OPEN_GL_H

swift_open_gl.cpp有这个:

#include "..\swift_open_gl.h"

inline Shader::Shader(const char * cs_path) {
    //Do stuff
}

我试过使用和不使用 inline(Visual Studio 在我尝试在 .h 文件和 .cpp 文件之间移动函数定义时添加了它,所以我决定尝试一下)并且我已经验证 #include <../swift_open_gl.h> 不会出现在项目中除上面列出的文件之外的任何其他地方。

我们将不胜感激!

同时提供 class 的默认构造函数。

class Shader {
    public:
    Shader(){} // default constructor
    Shader(const char* cs_path);    
    };

不要在 Shader::Shader(const char* cs_path){} 定义中使用内联