2

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

Header File, 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

Source file, 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() { ... }
}
}

I keep running into this issue where compiler the complains about the class name (Thing) not being a type. However, it is declared in the header file. From reading other posts, it seems like this issue is generally caused by failing to pre-declare a function or class. However, I don't see why my code does not accomplish this. I thought that it might be a namespace issue because GetStatus() will compile if I remove the namespace access (Thing::), but removing the namespaces did not help.

Max Feinberg
  • 810
  • 1
  • 6
  • 21
  • 1
    Do not use identifiers that start with an underscore followed by a capital letter like `_THING_H`. These are reserved for the implementation (compiler and standard library). – aschepler Jan 27 '18 at 01:50
  • I was unaware of that. Thank you for the information. – Max Feinberg Jan 27 '18 at 02:20

1 Answers1

1
#ifdef ABSTRACT_INTERFACE_FOR_THING

and

#ifdef _THING_H

should be:

#ifndef ABSTRACT_INTERFACE_FOR_THING
   ^

and

#ifndef _THING_H
   ^

And as suggested by @Amadeus, templates shouldn't be implemented in source files.

O'Neil
  • 3,790
  • 4
  • 16
  • 30