类名称没有使用模板化继承命名类型问题

时间:2018-01-27 00:36:49

标签: c++ templates inheritance namespaces

Pure Virtual Class,AbstractThing.hpp:

#ifdef ABSTRACT_INTERFACE_FOR_THING
#define ABSTRACT_INTERFACE_FOR_THING

namespace A
{
namespace B 
{

template <class T>
class AbstractThing
{
public:
  virtual ~AbstractThing() = 0;
  virtual T    GetStatus() = 0; 
};

}
}
#endif

头文件,Thing.hpp:

#ifdef _THING_H
#define _THING_H

#include "AbstractThing.hpp"
namespace A 
{   
namespace B 
{
template <class T>
class Thing : public AbstractThing<T> {
public:
  Thing();  
  ~Thing();
  T  GetStatus(); 
};

}
}

#endif

源文件,Thing.cpp,

#include "Thing.hpp"

namespace A
{
namespace B
{
template <class T>
Thing<T>::Thing() {}

template <class T>
Thing<T>::~Thing() {}

template <class T>
T Thing<T>::GetStatus() { ... }
}
}

我一直遇到这个问题,编译器抱怨类名(Thing)不是一个类型。但是,它在头文件中声明。从阅读其他帖子看,这个问题似乎通常是由于未能预先声明一个函数或类。但是,我不明白为什么我的代码没有实现这一点。我认为它可能是命名空间问题,因为如果我删除命名空间访问(Thing::),GetStatus()将编译,但删除命名空间没有帮助。

1 个答案:

答案 0 :(得分:1)

#ifdef ABSTRACT_INTERFACE_FOR_THING

#ifdef _THING_H

应该是:

#ifndef ABSTRACT_INTERFACE_FOR_THING
   ^

#ifndef _THING_H
   ^

正如@Amadeus所建议的,templates shouldn't be implemented in source files