0

Recently I came up with the situation, where I need to access templated parent methods from child, i.e.

class.hpp

template<size_t N> struct A {
    A();

    void some();
};

struct B : public A<1> {
    B();
};

class.cpp

#include "class.hpp"
#include <iostream>


template<size_t N> A<N>::A() {
    std::cout << "Created A" << std::endl;
}

template<size_t N> void A<N>::some() {
    std::cout << "Some" << std::endl;
}

B::B() {
    std::cout << "Created B" << std::endl;
}

main.cpp

#include "class.hpp"


int main() {
    B* ptr = new B();

    ptr->some();
}

But when I tried to execute it, I was provided with following error:

1>------ Build started: Project: test, Configuration: Debug Win32 ------  
1>class.cpp  
1>main.cpp  
1>Generating Code...  
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall A<1>::some(void)" (?some@?$A@$00@@QAEXXZ) referenced in function _main  
1>C:\Users\mkalu\Documents\vscode\Temp\VC++\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals  
1>Done building project "test.vcxproj" -- FAILED.  
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

So, I'm interested in a solution to this problem without need to destroy the overall hierarchy of templates & classes.

teviroff
  • 13
  • 3
  • Huge thanks! I didn't work with the **.tpp** file solution, but explicit class instantiation worked perfectly. – teviroff Oct 22 '22 at 16:35

0 Answers0