尝试编译我的代码时,链接器向 std::function 的复制构造函数发出未定义的引用错误。我不明白这怎么可能。这是我创建的 SSCCE。
#include <functional>
#include <cassert>
namespace Wide {
namespace Codegen {
class Statement { public: virtual ~Statement() {} };
class Expression : public Statement {};
class IfStatement : public Statement {};
class Generator {
public:
virtual IfStatement* CreateIfStatement(Expression* expr, Statement* t, Statement* f) = 0;
virtual IfStatement* CreateIfStatement(std::function<Expression*()>, Statement*, Statement*) = 0;
};
}
namespace LLVMCodegen {
class Statement { public: virtual ~Statement() {} };
struct Expression : Statement {};
class IfStatement : public Statement, public Codegen::IfStatement {
public:
IfStatement(std::function<LLVMCodegen::Expression*()> cond, LLVMCodegen::Statement* tbr, LLVMCodegen::Statement* fbr) {}
};
Expression* AssertExpression(Wide::Codegen::Expression* e) {
return dynamic_cast<Wide::LLVMCodegen::Expression*>(e);
}
Statement* AssertStatement(Wide::Codegen::Statement* e) {
return dynamic_cast<Wide::LLVMCodegen::Statement*>(e);
}
class Generator : public Codegen::Generator {
public:
IfStatement* CreateIfStatement(Codegen::Expression* expr, Codegen::Statement* t, Codegen::Statement* f) {
return CreateIfStatement([=] { return expr; }, t, f);
}
IfStatement* CreateIfStatement(std::function<Codegen::Expression*()> cond, Codegen::Statement* tr, Codegen::Statement* fls) {
return new IfStatement([=] { return AssertExpression(cond()); }, AssertStatement(tr), fls ? AssertStatement(fls) : nullptr);
}
};
}
}
int main() {
Wide::LLVMCodegen::Generator g;
}
尝试编译此示例给出
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall std::function<class Wide::Codegen::Expression * __cdecl(void)>::function<class Wide::Codegen::Expression * __cdecl(void)>(class std::function<class Wide::Codegen::Expression * __cdecl(void)> &)" (??0?$function@$$A6APAVExpression@Codegen@Wide@@XZ@std@@QAE@AAV01@@Z) referenced in function "public: class Wide::Codegen::IfStatement * __thiscall Wide::LLVMCodegen::Generator::CreateIfStatement(class std::function<class Wide::Codegen::Expression * __cdecl(void)>,class Wide::Codegen::Statement *,class Wide::Codegen::Statement *)" (?CreateIfStatement@Generator@LLVMCodegen@Wide@@QAEPAVIfStatement@Codegen@3@V?$function@$$A6APAVExpression@Codegen@Wide@@XZ@std@@PAVStatement@53@1@Z)
如果您从 中删除所有虚函数Wide::LLVMCodegen::Statement
,则代码示例将按应有的方式编译。如何在不引入无法使用界面的相当严重的问题的情况下解决此错误?