A C++ templated function issue

I have a problem with a Template function inside a class. When I call "Set" in something() function, VS show me: Error C2275 'T': illegal use of this type as an expression

The header is:

#include <vector>
#include <array>
#include <iostream>

using t_double = double;
template<typename T>
using t_vec = std::vector<T>;

class SuperPixel2
{
    t_vec<double> distances;

    template<typename T>
    void Set(t_vec<T> &v,
    size_t i,
    size_t j,
    const T &val);

    void something();
}

And the cpp file:

#include "SuperPixel2.h"
template<typename T>
void SuperPixel2::Set(t_vec<T> &v,
    size_t i,
    size_t j,
    const T &val)
{
    v[i * cols + j] = T;
}

void SuperPixel2::something()
{
    t_double d;
    //..
    Set(distances, k, l, (t_double)d);
    //..
}

Upvotes: 0

Views: 45

Answers (2)

sudo make install
sudo make install

Reputation: 5669

Well this line looks pretty odd:

v[i * cols + j] = T;

I think it's meant to be:

v[i * cols + j] = val;

As a side note (and maybe this would make more sense looking at the whole class), the type of distances is known (a vector of doubles), so it's unclear to me why the Set method needs to be a templated member.

Upvotes: 1

Tyler Lewis
Tyler Lewis

Reputation: 881

In addition to what sudo make install said, you generally cannot declare a template in a header file and write the implementation in a cpp file. See here for an answer to your problem.

Upvotes: 1

Related Questions