【发布时间】:2021-12-16 00:07:17
【问题描述】:
我有一个头文件,定义了chunk 类:
#pragma once
#include <vector>
#include "Tile.h"
#include "Numerics.h"
namespace boch {
class chunk {
public:
chunk();
static const uint defsize_x = 16;
static const uint defsize_y = 16;
std::vector<std::vector<tile*>> tilespace;
tile* getat(vint coords);
void fillc(tile t);
};
}
然后,我在Chunk.cpp文件中定义了类的实现:
#include "Chunk.h"
boch::chunk::chunk() {
tilespace = std::vector<std::vector<tile*>>(defsize_x);
for (int x = 0; x < defsize_x; x++) {
std::vector<tile*> temp = std::vector<tile*>(defsize_y);
tilespace[x] = temp;
}
}
void boch::chunk::fillc(tile t) {
for (int x = 0; x < defsize_x; x++) {
for (int y = 0; y < defsize_y; y++) {
tilespace[x][y] = new tile(t);
}
}
}
boch::tile* boch::chunk::getat(vint coords) {
return tilespace[coords.x][coords.y];
}
(vint 是 boch::vector<int> 的 typedef,它是自定义 X、Y 向量,如果有帮助的话)
然后,我在BochGrounds.cpp文件的main函数中使用它:
#include <iostream>
#include "Layer.h"
#include "Gamegrid.h"
int main()
{
boch::layer newlayer = boch::layer(boch::vuint(16, 16));
boch::chunk newchunk = boch::chunk();
boch::gamegrid newgrid = boch::gamegrid();
newchunk.fillc(boch::tile());
newgrid.addchunk(boch::cv_zero, &newchunk);
newgrid.drawtolayer(&newlayer);
newlayer.draw(std::cout);
}
Tile 类定义了 gamegrid 类,chunk 包括 tile 类,gamegrid 包括块和实体(其中也包括 tile)。图层类仅包括瓦片。所有头文件都有#pragma once 指令。尝试编译时,出现以下错误:
LNK2019 unresolved external symbol "public: __cdecl boch::chunk::chunk(void)" (??0chunk@boch@@QEAA@XZ) referenced in function main
LNK2019 unresolved external symbol "public: void __cdecl boch::chunk::fillc(class boch::tile)" (?fillc@chunk@boch@@QEAAXVtile@2@@Z) referenced in function main
结果:
LNK1120 2 unresolved externals
其他 StackOverflow 答案表明链接器无法看到 fillc() 和块构造函数的实现,但我不明白为什么它甚至是这里的问题。请帮忙。 (链接器设置未更改,是 MVSC 2019 的默认设置)
【问题讨论】:
-
你用什么命令来编译和链接程序?
-
要明确一点:您添加了
Chunk.cpp和BochGrounds.cpp是同一个 Visual Studio 项目的一部分,并且这两个文件实际上都是构建的? -
我会再次验证
Chunk.cpp实际上在项目中并且正在编译,而不仅仅是在与其他文件相同的目录中。直接包含您使用的标头是一种很好的做法,因此您希望在BochGrounds.cpp中包含“Chunk.h”。这不是你的错误的原因,但它会防止事情在未来可能发生的其他文件之一不包含它的情况下发生。 -
@fabian 是的,两个文件都在构建中。
-
@RetiredNinja 感谢您的建议!我实际上已经尝试过包含每个头文件,但遗憾的是它没有帮助。
标签: c++ linker-errors unresolved-external