链接器错误复制std :: function的构造函数

时间:2013-09-14 10:29:30

标签: c++ visual-studio-2012

当尝试编译我的代码时,链接器会向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中删除所有虚函数,则代码示例将按预期进行编译。如何在不引入无法使用接口的相当严重问题的情况下解决此错误?

1 个答案:

答案 0 :(得分:-1)

我发现通过制作参数const,我可以避免这个错误。