-2

for a personal project i need to define a template class for a better optimization.

I have followed exemple, but it doesn't compile yet cause i want my template class inherit from an interface. Can someone tell me how to use it, here is my code :

My dot HPP

template<typename T>
class Container : public IOP
{
public:
   Container();
   T val;
   int getpr() const;
   IOP *operator+(const IOP &r) const;
   IOP *operator-(const IOP &r) const;
   IOP *operator*(const IOP &r) const;
   IOP *operator/(const IOP &r) const;
}

My dot cpp where there is my functions (constructor ...)

Container::Container()
{

}

int Container::getpr()
{
...
}

... etc

And i want to use my class like that :

Container<long> test;

or

Container<int> test;

Thanks for any help, link or explanation.

compilation error for the moment :

Container.cpp:13:1: error: expected a class or namespace
Container::Container()
^
Container.cpp:13:12: error: C++ requires a type specifier for all declarations
Container::Container()
~~~~~~~~~  ^
Container.cpp:24:20: error: expected a class or namespace
std::string const &Container::toString()
                   ^
Container.cpp:29:5: error: expected a class or namespace
int     Container::getPrecision

()

Gabson
  • 421
  • 1
  • 4
  • 20
  • 3
    You cannot [define templates in a separate file](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – eerorika Feb 21 '14 at 13:38
  • even if its a HPP file ? so i have to write the whole class in the hpp file or the cpp ? – Gabson Feb 21 '14 at 13:39
  • You can define the template only in a file that is included by the code that instantiates the template. And things go very wrong if you include a cpp file. Technically, you can define templates in a separate (header) file if you include that file in the header which declares the template (as you can see in the first answer of the SO question that I linked). – eerorika Feb 21 '14 at 13:41
  • 1
    'i want my template class inherit from an interface' : you should not want that, if by 'interface' you mean an abstract base class. Templates define *implicit interfaces*, these things should not be mixed up. – tmaric Feb 21 '14 at 13:42
  • So i can't do that ? Or there is a way ? – Gabson Feb 21 '14 at 13:46

1 Answers1

1

Don't forget to mention <T> as this is template class implementation:

template <class T>
Container<T>::Container()
{

}

template <class T>
int Container<T>::getpr()
{
...
}

Because in your code it means you have declaration of template class Container<T> and implementation of non-template class Container

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42
  • Ok gonna try this. Before every class function implementation ? – Gabson Feb 21 '14 at 14:03
  • Absolutely. Doing so, you instruct compiler "this is implementation for template class X, method foo()". If you don't want to place such long form, you have to provide the implementation inplace, inside your declaration. – Yury Schkatula Feb 21 '14 at 15:40