我有一个在头文件中定义并在源文件中声明的方法。当我调用该方法时,链接器会抛出一个错误,说它找不到错误。
错误 1 错误 LNK2019:函数 _main 中引用的未解析的外部符号“public: class Chunk * __thiscall World::getChunk(short,short)”(?getChunk@World@@QAEPAVchunk@@FF@Z)
错误 2 错误 LNK1120: 1 未解决的外部
世界.h:
#pragma once
#include <map>
#include <vector>
#include "Chunk.h"
class World {
public:
World();
~World();
void generateChunk(short x, short z);
inline Chunk* getChunk(short x, short z);
private:
std::vector< std::vector<Chunk*> > loadedChunks;
};
世界.cpp:
#include "World.h"
#include <iostream>
World::World() : loadedChunks(1, std::vector<Chunk*>(1)) {}
World::~World() {
for(unsigned short x = loadedChunks.size(); x > 0; --x)
for(unsigned short z = loadedChunks[x].size(); z > 0; --z) {
std::cout << "Destructed" << std::endl;
delete loadedChunks[x][z];
}
}
void World::generateChunk(short x, short z) {
Chunk chunk(x, z);
delete loadedChunks[x][z];
chunk.generate();
loadedChunks[x][z] = &chunk;
}
Chunk* World::getChunk(short x, short z) {
return loadedChunks[x][z];
}
后来当我运行时:
World world;
world.generateChunk(0, 0);
world.getChunk(0, 0);
它不会编译/链接上述错误消息。