当使用其他类的构造函数时,Linker错误 "未解析外部符号"。

问题描述 投票:0回答:1

我一直在努力寻找为什么我的链接器得到一个未解决的外部符号错误。这个错误是这样的。

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-类的定义是在 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> 除了上面列出的文件外,在项目中没有其他地方出现。

如果能有多一双眼睛来观察这个问题,我将感激不尽!我一直在努力寻找为什么我的链接器会出现未解决的外部符号错误。

c++ include linker-errors unresolved-external
1个回答
1
投票

也提供类的默认构造函数。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.