mardi 26 février 2019

Class templates with std::array in separate files [duplicate]

This question already has an answer here:

How do i implement this class in separate files?

class MyClass {
public:
    template<class Type, std::size_t SIZE>                                 
    static void someFunc(std::array<Type, SIZE> &arr, const std::function<bool(Type, Type)> &pred) {
        //...
    } 
};

Here is what i try:

header.h

class MyClass
{   
public: 
    template<class Type, std::size_t SIZE>                                 
    static void someFunc(std::array<Type, SIZE> &arr, const std::function<bool(Type, Type)> &pred); 
};

header.cpp

template<class Type, std::size_t SIZE>                                
void MyClass::someFunc(std::array<Type, SIZE> &arr, const std::function<bool(Type, Type)> &pred) 
{
    //...
}

This stuff can be compiled. But if i try to use this i will see the next compile error:

LNK2019 unresolved external symbol "public: static void __cdecl MyClass::someFunc(class std::array &,class std::function const &)" (??$someFunc@H$04@MyClass@@SAXAAV?$array@H$04@std@@ABV?$function@$$A6A_NHH@Z@2@@Z) referenced in function _main

main.cpp

#include <iostream>
#include <array>
#include "MyClass.h"

int main() 
{
    std::array<int, 5> a{ 1, 2, 3, 4, 5 };
    MyClass::someFunc<int>(a, [](int a, int b) { return true; });
    std::cin.get();
}

Aucun commentaire:

Enregistrer un commentaire