c++ - 模板类给出链接器错误

标签 c++ templates


我有一个公开的模板类,我在其中添加了一个方法。这个类在命名空间 A 中。现在,我在另一个命名空间(比如 B)中调用这个方法。最初,编译器给我的链接器错误是针对此特定方法的“未解析的外部符号”。
但是,如果我在同一个命名空间(即 A)内调用此方法,它会很好地链接。之后,它在命名空间 B 中也能很好地链接。
为什么会这样?
这与我的类的创建模板对象有关吗?
阿图尔
这是代码片段......

namespace sss  
{  
    namespace AAA  
    {
        template <<typename T, typename TAlloc = Allocator< T > > 
        class DLL_EXPORT A  
        {  
             public:
             // Some other functions that are working fine ...
             bool fooA() const;  
             {  
                  return Size()>0;  
             }  
        };  
    }  
}  


//I get a linker error when I call it in another namespace (say B)...  
//I am accessing this method in public method of some other class in namespace B  

// Including the header for class A ...
#include A.h

namespace QQQ  
{  
    namespace B  
    {  
        class B
        {
            private:  
            AAA::A obj; // Object of class A ...
            public:  
            // SOme methods 
            // Method that calls fooA ...
            GetResult()
            { 
                fooA // This causes causes linker error when i call it here, 
                     // but, it works when i call it in namespace AAA, 
                     // and then it, it works here as well   
            } 
        };

当我从命名空间 B 中调用此方法时,它给出了未解析的外部符号,但是,当我从命名空间 A 中调用此方法时,它在 A 中的链接很好,在 B 中的链接也很好。
有任何想法吗 ?

最佳答案

关于c++ - 模板类给出链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4583816/

相关文章:

c++ - 删除的函数和 ADL

c++ - 在 C++ 的派生类构造函数中从基模板类访问变量

c++ - ISO C++ 标准 - 关于检查依赖库的规则。为什么?

c# - 为什么我可以从基于 C#/C++ 中该类型的模板化/泛型类派生

c++ - 为什么 long 和 long long 的最大值产生相同的输出?

c++ - 在 Windows 中获取另一个进程命令行

c++ - 当我有 Visual C++ 构建工具(不是 g++)时,如何为 VSCODE 设置包含路径库目录和链接器

c++ - EDSDK 3.4.0 on OS X 10.12.1 with Rebel t6i : `kEdsObjectEvent_DirItemCreated` event is not received for up to 30 seconds after photo is taken

python - Django 上下文不渲染

C++ 无效数组<T, N> 下标