0

I'm trying to create a DLL file with the following code: the header:


#pragma once

#ifdef PRO_ONE 
#define DLLFUNC _declspec(dllexport)
#else
#define DLLFUNC _declspec(dllimport)
#endif
#include <vector>
#include <string>
 
using namespace std;

template <class T>
class interAlgo {
public:
    virtual void summaryRanges(vector<int>& nums) = 0;
    virtual T getAlgoFirst() = 0;
    
};

template <class T>
class  firstSul : public interAlgo<T> {
private:
     T resultVec;
public:
     firstSul();
     void summaryRanges(vector<int>& nums);
     T getAlgoFirst();
    
};

template <class T>
extern "C" DLLFUNC  interAlgo<T>* _cdecl  CreateAlgoObject(); //issue is here!!!!!!!!!!!!!!!!

cpp file:

#include "pch.h"
#include "progone.h"
#include <sstream>
#include <map>

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

}

template <class T>
void firstSul<T>::summaryRanges(vector<int>& nums) {
    this->resultVec.clear();
    const int n = nums.size();

    int i = 0;

    for (int j = 0; j < n; ++j) {
        string range = "";
        if (j + 1 == n || nums[j] + 1 != nums[j + 1]) {
            if (i == j) {
                range = to_string(nums[i]);
            }
            else {
                range = to_string(nums[i]) + "->" + to_string(nums[j]);
            }
           resultVec.push_back(range);
            i = j + 1;
        }
    }
}

template <class T>
T firstSul<T>::getAlgoFirst() {
    return this->resultVec;
}


template <class T>
interAlgo<T>* CreateAlgoObject() {
    return new firstSul<T>();
}

the error's are: E0335 linkage specification is not allowed C2955 'interAlgo': use of class template requires template argument list

Why is this happening, and how can the problem be fixed?

starball
  • 20,030
  • 7
  • 43
  • 238
zeke
  • 1
  • 1
  • Does this answer your question? [C++ instantiate template class from DLL](https://stackoverflow.com/questions/14138360/c-instantiate-template-class-from-dll) – Daniel Waechter Dec 22 '22 at 16:25
  • 1
    Is there any reason why you haven't implemented your templates in the header file? Doing that would make all your problems go away. – Paul Sanders Dec 22 '22 at 16:30
  • It's not template class, it's class template. You can't export template, because it's not a type (yet). You can, however, export template specializations. – Osyotr Dec 22 '22 at 20:02

0 Answers0