在Mac XCode上实例化C ++类

时间:2016-04-08 00:31:05

标签: c++ templates

我正在尝试在Mac上运行一些旧代码。代码使用模板类

我有一个包含模板类定义的.h文件。 我有一个包含模板成员定义的.cpp文件。

然后我为每种类型都有一个.cpp文件。

#include "template.h"
#include "template.cpp"

MyClass<whatevertype>  dummyobject ;

这与我多年前使用过的其他编译器一起工作过。但是,XCode为缺少的类定义提供了链接器错误。

我在template.cpp文件上收到警告:

  1. 我最初拥有使用EXPORT限定的模板成员函数。这给了编译器警告(忽略)。
  2. 根据此板上的一些答案,我将EXPORT更改为INLINE。这给了警告它被忽略了。
  3. 我有template.cpp文件中的模板成员函数引用的非模板函数。我得到一个警告,说它们没有被引用和被忽略(表示模板类没有被实例化)。
  4. 我想知道我需要在最大的XCode中使用什么过程才能让模板类实例化。

    让我试着给读者&#39;摘要版。这是我多年前在Windoze上用过几年前实例化模板类的安排。它不适用于Mac。我得到了成员的未定义错误。

    template.h
    // Defines the template class. Highly simplified example shown.
    template class MyClass<unsigned int SIZE>
    {
    public:
        void someMethod() ;
    } ;
    
    template.cpp
    // Defines the template class member functions. 
    
    #include "template.h"
    inline void doSomething ()
    {
     // XCode gives warning that doSomething is not generated. Providing another
     // example that the class is not being instantiated.
    }
    
    {inline | export} void template MyClass<unsigned int SIZE>::someMethod ()
    {
       for (unsigned int ii = 0 ; ii < SIZE ; ++ ii)
             doSomething () ;
    }
    
    myinstance.cpp
    // Attempt to instantiate
    #include "template.cpp"
    MyClass<42> dummyObject ;
    

0 个答案:

没有答案
相关问题