Question

I'm trying to deal with namespaces & templates in C++. I can get the following code to compile in MSVC (no warnings or errors), but am having no luck at all with various permutations with CYGWIN/GCC. Any help would be appreciated.

In the header file I declare a templated subclass as follows:

#include <gdal.h>
namespace sfms {

template <class _type, GDALDataType _gdal> class SmfsGrid_Typed : public SfmsGrid_Base {
public:
  SmfsGrid_Typed();
  SmfsGrid_Typed(const SmfsGrid_Typed<_type, _gdal> *toCopy);
  SmfsGrid_Typed(std::string filename);
  virtual ~SmfsGrid_Typed();
  virtual bool OpenRead();
  virtual bool OpenWrite();

protected:
  _type m_nodata_value;

  virtual SfmsGrid_Base *New() const;
  virtual SfmsGrid_Base *New(SfmsGrid_Base *toCopy) const;
  virtual void initCopy(SfmsGrid_Base *copy) const;
};

template SmfsGrid_Typed<double, GDT_Float64>;
template SmfsGrid_Typed<float, GDT_Float32>;
template SmfsGrid_Typed<int, GDT_Int32>;

typedef SmfsGrid_Typed<double, GDT_Float64> SmfsGrid_Double;
typedef SmfsGrid_Typed<float, GDT_Float32> SmfsGrid_Float;
typedef SmfsGrid_Typed<int, GDT_Int32> SmfsGrid_Int;
}

In the source file I instantiate the specialized template class as follows:

void hi_there() {
//...
sfms::SmfsGrid_Typed<int, GDT_Int32> *grid = new sfms::SmfsGrid_Typed<int, GDT_Int32>(filey);
//...
sfms::SmfsGrid_Int *grid2 = new sfms::SmfsGrid_Int(filey);
//...
}

GDALDataType is an enum, but that doesn't seem to the the issue.

I've tried the class declaration inside and outside the namespace, with no success.

The source file containing the implementations for the templates compile okay with both compilers.

I've tried dropping the explicit template instantation and including the relevant C++ source file, also with no joy.

I've tried 'template', 'typename' and 'typedef' keywords in various different places (in the templated class def'n and where I try to create the object) with no success, but various interesting and often misleading error messages with GCC, such as:

error: 'SmfsGrid_Typed' is not a member of 'sfms'

when it clearly is! :) Regardless, any help on porting this code from MSVC to GCC would help.

Thanks!

Was it helpful?

Solution

Your explicit template instantiation looks wrong. Try replacing it with

template class SmfsGrid_Typed<double, GDT_Float64>;
template class SmfsGrid_Typed<float, GDT_Float32>;
template class SmfsGrid_Typed<int, GDT_Int32>;

(Note the added class keyword)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top