未解析的外部符号,但函数已定义并实现
Unresolved external symbol but the function is defined and implemented
我有一个头文件,定义了chunk
class:
#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
文件中定义了class的实现:
#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>
的类型定义,它是自定义 X、Y 向量,如果有帮助的话)
然后,我在 BochGrounds.cpp
文件的主要函数中使用它:
#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 class 定义 gamegrid class,chunk 包括 tile class,gamegrid 包括 chunk & entity(也包括 tile)。图层 class 仅包含图块。所有头文件都有 #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
其他 Whosebug 答案表明链接器看不到 fillc()
和块构造函数的实现,但我不明白为什么它甚至是这里的问题。请帮忙。 (链接器设置未更改,并且是 MVSC 2019 的默认设置)
感谢糖糖的回答。我删除了头文件和 .cpp 文件并重新添加了它们,效果非常好。我想我只是通过直接将新文件添加到 header/source 文件夹而不是将其添加到项目来添加头文件或 .cpp 文件(点击项目 > 添加新项目)。
我有一个头文件,定义了chunk
class:
#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
文件中定义了class的实现:
#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>
的类型定义,它是自定义 X、Y 向量,如果有帮助的话)
然后,我在 BochGrounds.cpp
文件的主要函数中使用它:
#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 class 定义 gamegrid class,chunk 包括 tile class,gamegrid 包括 chunk & entity(也包括 tile)。图层 class 仅包含图块。所有头文件都有 #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
其他 Whosebug 答案表明链接器看不到 fillc()
和块构造函数的实现,但我不明白为什么它甚至是这里的问题。请帮忙。 (链接器设置未更改,并且是 MVSC 2019 的默认设置)
感谢糖糖的回答。我删除了头文件和 .cpp 文件并重新添加了它们,效果非常好。我想我只是通过直接将新文件添加到 header/source 文件夹而不是将其添加到项目来添加头文件或 .cpp 文件(点击项目 > 添加新项目)。