【问题标题】:Unresolved external symbol but the function is defined and implemented未解析的外部符号,但功能已定义和实现
【发布时间】: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];
}

vintboch::vector&lt;int&gt; 的 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.cppBochGrounds.cpp 是同一个 Visual Studio 项目的一部分,并且这两个文件实际上都是构建的?
  • 我会再次验证Chunk.cpp 实际上在项目中并且正在编译,而不仅仅是在与其他文件相同的目录中。直接包含您使用的标头是一种很好的做法,因此您希望在BochGrounds.cpp 中包含“Chunk.h”。这不是你的错误的原因,但它会防止事情在未来可能发生的其他文件之一不包含它的情况下发生。
  • @fabian 是的,两个文件都在构建中。
  • @RetiredNinja 感谢您的建议!我实际上已经尝试过包含每个头文件,但遗憾的是它没有帮助。

标签: c++ linker-errors unresolved-external


【解决方案1】:

感谢糖的回答。我删除了头文件和 .cpp 文件并重新添加了它们,它就像一个魅力。我想我已经添加了 header 或 .cpp 文件,只是直接将新文件添加到 header/source 文件夹而不是将其添加到项目中(人民币点击项目>添加新项目)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多