我有一种感觉,我错过了一些非常愚蠢的东西,但仍然:我在写的游戏引擎中有这个烦人的“外部符号”错误:
基本上我想创建一个在一些全局变量中读取路径的类(所以我不必将它们全部发送到那里)。我使用github的NFD(nativefiledialog)来打开文件。我在此之前直接在main.cpp中对它进行了测试,但是只有在将它放入类之后才会出现问题。
https://github.com/mlabbe/nativefiledialog
Paths.h
#pragma once
#include <iostream>
#include <nfd.h>
namespace RW {
class Paths {
private:
static nfdchar_t *gamePath;
static nfdresult_t result;
public:
static void chooseGamePath();
};
}
Paths.cpp
#include "Paths.h"
namespace RW {
nfdchar_t Paths::*gamePath = NULL;
nfdresult_t Paths::result;
void Paths::chooseGamePath()
{
result = NFD_OpenDialog(NULL, NULL, &gamePath);;
std::cout << "Please choose the location of the warcraft's exe file!" << std::endl;
if (result == NFD_OKAY) {
std::cout << "Success!" << std::endl;
std::cout << gamePath << std::endl;
free(gamePath);
}
else if (result == NFD_CANCEL) {
std::cout << "User pressed cancel." << std::endl;
}
else {
std::cout << "Error: " << NFD_GetError() << std::endl;
}
}
}
错误:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "private: static char * RW::Paths::gamePath" (?gamePath@Paths@RW@@0PADA) Half-an Engine D:\Programozás\Repositories\Warcraft-II-HD\Half-an Engine\Paths.obj 1
在cpp文件中,这一行:
nfdchar_t Paths::*gamePath = NULL;
声明一个名为gamePath
的指向成员的指针,它只能指向Paths
类的nfdchar_t
类的成员。
但这并不是你在gamePath
课上宣布的Paths
成员。您将其声明为一个简单的(静态)nfdchar_t*
指针。
改为将该行改为:
nfdchar_t* Paths::gamePath = NULL;
这声明了一个名为gamePath
的变量,它是Paths
类的成员,类型为nfdchar_t*
。这符合你在gamePath
类声明中对Paths
的声明。