2

We were trying to define a class using a template:

template<typename _VType, size_t _Len>
class Obc<_VType,_Len> {
  private:
    size_t len = _len;
  public:
    Obc();
    Obc(_VType *values);
    ...
};

and, we expected to be able to use it like:

Obc<_DType, 2> m = new Obc();

However, "‘Matrix’ is not a class template" when compiling.

We tried sols found by searching 'not a class template', like 'X is not a template' error , but no luck yet

Any thoughts?

AndyG
  • 39,700
  • 8
  • 109
  • 143
  • 3
    Nothing you've provided alludes to a `Matrix` class. It seems you failed to supply some important snippets. – François Andrieux Sep 21 '17 at 18:05
  • 4
    When you are defining a class, you don't need the template parameters after the class name (`class Obc<_VType,_Len>`) unless you are defining a specialization. – 0x5453 Sep 21 '17 at 18:05
  • you are using new on a variable instead on a pointer – JPX Sep 21 '17 at 18:27
  • Note that any name at any scope starting with an underscore followed by a capital letter, such as `_DType` is reserved to the standard. – Guillaume Racicot Sep 21 '17 at 19:59

2 Answers2

7

Don't repeat your template parameters:

template<typename _VType, size_t _Len>
class Obc {
  ...
};

And assuming that _DType is an existing type, use your template class with template parameters and without using the java syntax, for example:

Obc<_DType, 2> m;
Obc<_DType, 2> mat{values};    // assuming _DType *values point to something
auto othermat = mat; 

Online demo

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Also, `size_t len = _len;` should be `size_t len = _Len;`, or even `const size_t len = _Len;` (since `_Len` can only be initialized with a constant known at compile-time) – Remy Lebeau Sep 21 '17 at 18:32
  • @Christophe, I added my answer because I thought it useful to mention the reason for the error, which is that it considers Obc a specialization – Werner Erasmus Sep 21 '17 at 18:36
  • Really sorry for haven't mention this is a C++ 11 code – user1274704 Sep 22 '17 at 02:48
  • So, [Live Demo](https://ideone.com/rUi5jd) is a slightly modified version based on @Christophe 's code. – user1274704 Sep 22 '17 at 03:07
  • After that, we attempts two extensions. First is to use points for the `data`, code hosted in [Points Demo](https://ideone.com/kcqaYf) ('Runtime error' in the site somehow ), which produce sth. like `Object with len 6 = [ 2 0 3 ; 0 881853936 32767 ]` for the `othermat.show()`. This is confusing. Shouldn't `othermat` just `copy` the instance `mat` entirely? – user1274704 Sep 22 '17 at 03:32
0

We were trying to define a class using a template:

template<typename _VType, size_t _Len>
class Obc<_VType,_Len> {
  private:
    size_t len = _Len;
  public:
    Obc();
    Obc(_VType *values);
    ...
};
and, we expected to be able to use it like:

The compiler in use thinks that Obc is a specialization, which it is not, and hence the error is 'X is not a template':

The example below explains it more clearly (see comments):

template <typename VType, size_t Len> 
class Obc { //Non-specialized
  private:
    size_t len = Len;
  public:
    Obc(){}
    Obc(VType *values){}
};

template<typename VType>
class Obc<VType,5> { //Partially specialized for size_t = 5
  //... expects non-specialized declaration
  private:
    size_t len = 5;
  public:
    Obc(){}
    Obc(VType *values){}
};

class X{};

int main() {
  // Using new like this... bad style... leak...
  auto m = new Obc<X, 5>(); //Instantiates specialization
  auto n = new Obc<X, sizeof(X)>(); //Instantiates original
  // your code goes here
  return 0;
}

BTW, using new to create Obc implies that it is created on the heap, which is (1) not always necessary, and (2), when done, should be de-allocated. In that case it should be wrapped with some kind of smart_ptr, such as unique_ptr or shared_ptr, depending on ownership semantics:

Rather do (in the above example):

...
  Obc<X, 5> m{}; //Instantiates specialization
//or
  auto ptr = std::make_shared<Obc<X,5>>(); 
  //...Or some other smart pointer type
...
Werner Erasmus
  • 3,988
  • 17
  • 31