0

So I have base class with template function. I want to call that function from derived class with type specified in that derived class. Base class must not know about type, i'm going to provide to the template function, also the definition of template function should not be in the base header file. At the moment my architecture looks as follows:

Base.h

class Base
{
public:
    Base();
    
    template<typename T>
    void func(T value);
};

Base.cpp

Base::Base()  
{
    cout << "Base()" << endl;
}

template<typename T>
void Base::func(T value)
{
    cout << "Base::func()" << endl;
}

Derived.h

class Derived : public Base
{
public:
    Derived();
    void callFunc();
};

Derived.cpp

Derived::Derived()
{
    cout << "Derived()" << endl;
}

void Derived::callFunc()
{
    Base::func(5);
}

template
void Base::func<int>(int value); // line of the error

But I am getting the error: error: explicit instantiation of ‘void Base::func(T) [with T = int]’ but no definition available [-fpermissive]

So how can I resolve this problem?

RomaFUN
  • 49
  • 6
  • That template member implementation belongs in the header for this to work as you seem to intend. Derived.cpp has no clue what the implementation of `Base::func` looks like; it cannot explicitly instantiate what it doesn't know the roadmap for. – WhozCraig May 18 '22 at 09:00
  • Even if it would not the case that the code must be visible, why people tend to split all and everything to cpp files. Is compile time still a problem today? If no LTO is active, this kind of coding will avoid a lot of optimizations and result in bad generated code. Why we see this here day by day? – Klaus May 18 '22 at 09:03
  • @Klaus because people learn from crappy websites, poor (or no) texts, and instructors who are they themselves poorly versed in the language and standard library, and/or using out-dated materials from 20+ years ago. – WhozCraig May 18 '22 at 09:05
  • @RichardCritten i know, that i can just make definition of the template func in the header file, but i don't really want to do it, so mentioned it in the question "also the definition of template function should not be in the base header file" – RomaFUN May 18 '22 at 09:12
  • The same with bits.h includes and turbo C compilers... Sometimes I believe I look into a museum if I see people asking here. And why such "professors" are still active? Is there nothing which can stop them? If I see what our co workers deliver, I know that this is not a problem from some people, in some country's that seems to be standard for, as you said, 20+ years. Is it not IT business which is always up to date? :-) :-) – Klaus May 18 '22 at 09:13
  • @RomaFUN you can try explicit instantiation in the cpp file for __all/every__ valid template parameters or move it to the header file. Templates are not expanded until needed and the compilation unit needing to do the expansion needs to see the complete definition (of the template) ie `derived.cpp` will be doing the template expansion and needs to see the source of the template to be able to expand it. – Richard Critten May 18 '22 at 09:14
  • 1
    @Klaus don't know about the others, but for me it's more comfortable to place definitions in the cpp file, not header. This way code looks more beautiful (as for me) – RomaFUN May 18 '22 at 09:14
  • @RichardCritten please read the question more carefully. I asked: can I reach the goal with 2 conditions: 1. place definition of template func in cpp file and 2. base class shouldn't know about template parametres types – RomaFUN May 18 '22 at 09:17
  • 2
    @RomaFUN No you can't - I have tried to give you complete information. Asking me to re-read the Q to get a different answer is not helpful. – Richard Critten May 18 '22 at 09:18
  • But coding is something technical and not about beautiful text style. You have to know what the results of this "technical bad style" will be. At the end of the day your personal style will consume more disk space, more ram/rom and at least a lot more electric power. Only for style? – Klaus May 18 '22 at 09:19
  • @Klaus i think there is some middle ground between technically correct code and beautiful code – RomaFUN May 18 '22 at 09:24
  • A "solution" to strictly "not in the header file" would be to have a third file - `Base.templates` with the template definition, and then include *that* in the Derived.cpp. A lot uglier IMO, but that's a matter of taste. – BoP May 18 '22 at 09:28

0 Answers0