首页 > 解决方案 > Visual C++ - 使用模块和概念时未解析的外部符号

问题描述

我正在尝试学习 C++,并且一直在使用玩具游戏引擎测试新的模块系统。不幸的是,我不断收到的这个链接错误什么也没告诉我,我也不明白我做错了什么。我真正能弄清楚的是,主函数看不到播放功能的实现,但我不知道如何修复它。我已将我的项目缩减到最低限度,因此希望对有经验的 C++ 程序员来说更清楚。任何输入表示赞赏,谢谢。

主文件

import engine;

int main() {
    engine::game<engine::text_ui> my_game("My Game");
    my_game.play();
}

引擎.ixx

export module engine;

export import :game;
export import :ui;

游戏.ixx

export module engine:game;

export import <concepts>;
export import <string>;
export import :ui;

namespace engine {
    export template<typename ui_t>
    requires std::derived_from<ui_t, ui>
    class game {
        std::string name;
        ui_t ui;
    public:
        game(std::string name) : name{name} {};
        void play() const;
    };
}

游戏.cxx

module engine:game;

import <concepts>;
import :ui;

namespace engine {
    template<typename ui_t>
    requires std::derived_from<ui_t, ui>
    void game<ui_t>::play() const {
        ui.notify(name + " is being played.\n");
    }
}

ui.ixx

export module engine:ui;

export import <string>;
export import <iostream>;

namespace engine {
    export class ui {
    public:
        virtual void notify(std::string text) const = 0;
    };

    export class text_ui : public ui {
        std::istream& in{std::cin};
        std::ostream& out{std::cout};
    public:
        void notify(std::string text) const override { out << text; };
    };
}

错误

LNK2019 unresolved external symbol
"public: void __thiscall engine::game<class engine::text_ui>::play(void)const "
(?play@?$game@Vtext_ui@engine@@@engine@@QBEXXZ::<!engine>)
referenced in function _main

标签: c++visual-c++linker-errorsc++20

解决方案


推荐阅读