【问题标题】:Linking error in C++ - implementing a indexListC++ 中的链接错误 - 实现 indexList
【发布时间】:2009-04-07 01:01:32
【问题描述】:
Linking... 
Directory.obj : error LNK2019: unresolved external symbol "public: void __thiscall indexList<class entry,100>::read(class std::basic_istream<char,struct std::char_traits<char> > &)" (?read@?$indexList@Ventry@@$0GE@@@QAEXAAV?$basic_istream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _main

收到此错误以及与 indexList 实现相关的其他错误。我已包含所有正确的文件,不知道这意味着什么?

indexList.h
indexList.cpp

另外,使用 VS .NET 2003 - 它们位于“源文件”和“头文件”下但是,我测试了删除 indexLish.h 并且错误没有改变?

【问题讨论】:

    标签: c++ symbols


    【解决方案1】:

    您拥有的是一个类模板。这意味着当编译器需要调用一个函数时,它会查看你的模板定义并根据需要生成相应的代码。

    例如,如果尝试调用以下可能会出现编译时错误:

    template <typename T>
    void doSomething(const T& x) {
        x->_this_function_does_not_exist_ever_();
    }
    

    但只要你不调用doSomething,你就不会出错。

    您遇到的问题是您的头文件告诉编译器“嘿,这些函数存在”,但是当编译器尝试生成它们时,它找不到任何定义。 (您不能“编译”源文件中的定义并将它们链接起来,它们必须对调用者可见。)

    最常见的解决方案是在.h 或.hpp 文件中简单地定义整个类模板。

    【讨论】:

      【解决方案2】:

      您使用的是 Visual Studio,然后将这两个文件都包含到解决方案中,然后运行。

      【讨论】:

      • VS .NET 2003 - 它们在“源文件”和“头文件”下但是,我测试了删除 indexLish.h 并且错误没有改变?
      • 你在cpp中定义了两次read方法?模板 void indexList::read(istream& ins) 模板 void indexList::read(istream& ins)
      【解决方案3】:

      由于您使用的是模板,因此最好的方法是将定义包含在 .H 文件中。

      我从book 中读到了一些东西。这里是something,它也可以帮助你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多