Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:01:38+00:00 2026-06-11T00:01:38+00:00

Possible Duplicate: C++ template, linking error I have two linking errors and I have

  • 0

Possible Duplicate:
C++ template, linking error

I have two linking errors and I have no idea what’s wrong with the code and how to fix them:

main.obj:-1: error: LNK2019: unresolved external symbol “public:
__thiscall A::A(void)” (??0?$A@VB@@@@QAE@XZ) referenced in function “public: __thiscall B::B(void)” (??0B@@QAE@XZ)

and

main.obj:-1: error: LNK2019: unresolved external symbol “public: void
__thiscall A::exec(void (__thiscall B::*)(void))” (?exec@?$A@VB@@@@QAEXP8B@@AEXXZ@Z) referenced in function “public:
void __thiscall B::run(void)” (?run@B@@QAEXXZ)

Explaining the code a little:
This class has to execute a function from the derived class. function exec is called from the derived class with a function from the derived class parameter. Signature of this function is void function();

//header.h

#ifndef HEADER_H
#define HEADER_H

template <class T>
class A
{
public:
    typedef void (T::*ExtFunc)();

    A();
    void funcA();
    void exec(ExtFunc func);
};

#endif // HEADER_H

//header.cpp

#include "header.h"

template<typename T>
A<T>::A() { }

template<typename T>
void A<T>::funcA()
{
    cout << "testA\n";
}

template<typename T>
void A<T>::exec(ExtFunc func)
{
    (T().*func)();
}

In main.cpp I derive a class from A class and pass the derived class as template paramtere. Then I execute function exec through the run() function.
//main.cpp

#include <iostream>
#include "header.h"
using namespace std;

class B : public A<B>
{
public:
    B() { }

    void run()
    {
        exec(&B::funcB);
    }

    void funcB()
    {
        cout << "testB\n";
    }
};

int main()
{
    B ob;
    ob.run();

    return 0;
}

Can anyone tell me what’s going on?…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:01:39+00:00Added an answer on June 11, 2026 at 12:01 am

    When you are using templates, generally you cannot put the implementation in a .cpp file – you have to put the whole class in the header. So move all the code from header.cpp to the .h.

    You can get around this by doing an explicit instantiation inside the .cpp file – instantiating the template for a particular type. But this requires that you know ahead of time which types will need an instantiation and will prevent you from adding new instantiations. The only benefit is a reduction in compile time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: template<> in c++ I have seen template<> in c++ code. Is this
Possible Duplicate: Confusing Template error I have a templated class with a templated method.
Possible Duplicate: C++ Inherited template classes don't have access to the base class I'm
Possible Duplicate: Where and why do I have to put the “template” and “typename”
Possible Duplicate: problem with template inheritance This code doesn't compile in GCC: template <typename
Possible Duplicate: Where and why do I have to put the “template” and “typename”
Possible Duplicate: Where and why do I have to put the “template” and “typename”
Possible Duplicate: C++ invoke explicit template constructor First imagine I have a class Data
Possible Duplicate: Where and why do I have to put the “template” and “typename”
Possible Duplicate: Where and why do I have to put the “template” and “typename”

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.