【问题标题】:Template class - unresolved external symbol(s) [duplicate]模板类 - 未解析的外部符号 [重复]
【发布时间】:2011-08-12 05:20:04
【问题描述】:

我经常遇到这个错误,但我不知道为什么。有人可以帮我找出原因吗?

编辑:删除的代码

【问题讨论】:

    标签: c++ class templates


    【解决方案1】:

    将实现(您的方法定义)与类声明一起放入标题中(请参阅this in the C++ FAQ)。一些编译器支持“export”关键字以按照您的方式执行此操作,但在 C++0x 中已被取消。

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        发生此错误的原因是,您的template 定义对用户代码不可见。模板定义应在中声明,

        1. 头文件,连同模板 声明
        2. 在 .cpp 文件中,用户 代码使用它。然而在这 接近定义将是 仅对使用的 .cpp 可见 它。见下面的例子

        test.h

        template<typename T>
        void foo (T*);
        

        test.cpp

        int main ()
        {
          foo(1);
        }
        
        template<typename T>
        void foo (T *p)
        {
         ...
        }
        

        test2.cpp

        // This file can not see the definition of foo
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-12
          • 2020-03-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多