vienya
vienya

Reputation: 57

Template specialization with void function

I'm working on a little program where i create a 2d array. The function itself works fine, but i get a problem when i want to implement the function in its own file. This is how it looks like:

mat.cpp:

#include <iostream>
#include <iomanip>
#include "matdyn.hpp";

int main(){
    int row, column;
    cin >> row;
    cin >> column;

    int** M1;
    double** M2;

    reserve(M1, row, column);
    reserve(M2, row, column);

    return 0;
}

matdyn.hpp

#ifndef dyn
#define dyn
template <typename T, typename S>
void reserve(S **, T, T);
#endif

matdyn.cpp:

#include "matrix_dyn.hpp"

template <typename S, typename T>
void reserve(S **&x, T row, T column){
    x = new S*[row];
    for(int i = 0; i < row; ++i) {
        x[i] = new S[column];
    }
}  

template void reserve<int, int, int>(int, int, int);
template void reserve<double, int, int>(double, int, int);

My problem is the last part of the matdyn.cpp. I always get error messages like:

error: template-id ‘reserve<int, int, int>’ for ‘void reserve(int, int, int)’ does not match any template declaration    
template void reserve<int, int, int>(int, int, int);

How do i write these last two lines properly? Thank you for any help!

Upvotes: 0

Views: 677

Answers (3)

Dimitrios Bouzas
Dimitrios Bouzas

Reputation: 42899

You have to either put the definition of your template function to the header matdyn.hpp

#ifndef dyn
#define dyn
template <typename S, typename T>
void reserve(S **&x, T row, T column){
    x = new S*[row];
    for(int i = 0; i < row; ++i) {
        x[i] = new S[column];
    }
}  

template void reserve<int, int>(int**&, int, int);
                      ^^^^^^^^  ^^^^^^
template void reserve<double, int>(double**&, int, int);
                      ^^^^^^^^^^^  ^^^^^^^^^

#endif

Or include the matdyn.cpp in matdyn.hpp

#ifndef dyn
#define dyn
template <typename T, typename S>
void reserve(S **, T, T);

#include "matdyn.cpp"

#endif

As for the reason look this SO question

Mind also the errors of your template specializations. The template parameter list of a function template specialization must match the template parameter list of the function template.

Upvotes: 0

coincoin
coincoin

Reputation: 4685

There are several problems with your code :

Your function reserve definition and declaration do not match.

I believe you wanted to write :

template <typename T, typename S>
void reserve(S **x, T row, T column){

Regarding your explicit template function instantations it should be done like this :

template void reserve<int, int>(int **, int, int);
template void reserve<int, double>(double **, int, int);

You have to match the template parameters given in your function declaration.

Here a live code which compiles.

Upvotes: 2

skypjack
skypjack

Reputation: 50540

Quote from here:

The definition of a template must be visible at the point of implicit instantiation, which is why template libraries typically provide all template definitions in the headers (e.g.most boost libraries are header-only)

Because of that, you should rather put everything in the header file, for that's the most common approach.

Upvotes: 0

Related Questions