Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;抽象类模板&x2B;类型特定子类=链接器有问题_C++_Inheritance_Templates - Fatal编程技术网

C++ C++;抽象类模板&x2B;类型特定子类=链接器有问题

C++ C++;抽象类模板&x2B;类型特定子类=链接器有问题,c++,inheritance,templates,C++,Inheritance,Templates,讨论中的项目涉及不同端点之间的通信。端点发送事件(超出当前问题的范围)并可以处理传入事件。每个事件在通用对象中表示如下: #pragma interface ... // some includes template<typename T> class Event { public: Event(int senderId, Type type, T payload); // Type is an enum

讨论中的项目涉及不同端点之间的通信。端点发送事件(超出当前问题的范围)并可以处理传入事件。每个事件在通用对象中表示如下:

#pragma interface
... // some includes

template<typename T>
class Event
{
   public:
                       Event(int senderId, Type type, T payload); // Type is an enum
                       Event(int senderId, Type type, int priority, T payload);
      virtual          ~Event();
      virtual int      getSenderId();
      virtual int      getPriority();
      virtual T        getPayload();
      void             setPriority(const int priority);

   protected:
      const int        senderId;
      const Type       type;
      const T          payload;
      int              priority;
};
#pragma interface
#include "Event.h"

template<typename T>
class AbstractEndPoint
{
   public:
                        AbstractEndPoint(int id);
      virtual           ~AbstractEndPoint();
      virtual int       getId();
      virtual void      processEvent(Event<T> event) = 0;    

   protected:
      const int         id;
};
分别。后一类定义为

class ConcreteEndPoint : AbstractEndPoint<TelegramFormatA>
{
    ...
}
class ConcreteEndPoint:AbstractEndPoint
{
...
}
我使用的是g++4.4.3和LD2.19。一切都可以很好地编译,但是链接器抱怨没有定义对特定类型事件类的引用,比如

Event<TelegramFormatA>::Event(....) .
Event::Event(..)。
我尝试使用

template class AbstractEndPoint<TelegramFormatA>;
模板类抽象端点;
但无法通过上述链接器错误


任何想法都将不胜感激。

函数模板和类模板的成员函数必须在头文件中实现,而不是在.cpp文件中实现。我想您在.cpp文件中实现了
Event::Event()

函数模板和类模板的成员函数必须在头文件中实现,而不是在.cpp文件中实现。我想您在.cpp文件中实现了
Event::Event()

正如sbi alread指出的,不允许在源文件和头文件中分离模板的接口和实现。查看此链接了解详细信息(页面末尾)

正如sbi alread指出的,不允许在源文件和头文件中分离模板的接口和实现。查看此链接了解详细信息(页面末尾)

我刚刚找到解决方案。信不信由你,它与GNU链接器(ld)的v2.20完美链接,但在ld 2.19中却无法做到这一点

顺便说一下

#pragma interface 


是一个特定于g++的解决方法,用于解决在头文件中实现模板类的问题。使用pragma标记是可以避免的,所以这不是问题所在,但还是要感谢您的建议。

我刚刚找到了解决方案。信不信由你,它与GNU链接器(ld)的v2.20完美链接,但在ld 2.19中却无法做到这一点

顺便说一下

#pragma interface 


是一个特定于g++的解决方法,用于解决在头文件中实现模板类的问题。使用pragma标记是可以避免的,因此这不是问题所在,但还是感谢您的建议。

允许在.cpp文件中包含实现,但必须将其包含在头文件中,以便可以看到实现。它也不能自己编译。以这种方式组织时,通常使用不同的文件扩展名(通常为.inc)。允许在.cpp文件中包含实现,但必须将其包含在头文件中,以便实现可见。它也不能自己编译。以这种方式组织时,通常使用不同的文件扩展名(通常为.inc)。
#pragma interface 
#pragma implementation