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

  • SEARCH
  • Home
  • 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 8929533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:41:25+00:00 2026-06-15T08:41:25+00:00

Possible Duplicate: Why do I get unresolved external symbol errors when using templates? I

  • 0

Possible Duplicate:
Why do I get “unresolved external symbol” errors when using templates?

I have a little bit complicated set of classes.
Class _A which has child parametrized class A which has two children A1 and A2.
Class B. Contains pointer on object of class _A as member. Has two child classes B1 and B2 which correspond to classes A1 and A2 each. B1 constructs _A as A1. B2 as A2 respectivelly.
And finally class Y which has child BY defined inside of class B.
Now how it is present in files.

tf1.h

#include <iostream>

struct Y{ // goes to the file tf2.h as ancestor of the class B::BY
};


struct _A{ // goes to the file tf2.h as a member of the class B
};

template<class X>
struct A: public _A {  // goes to two classes below only as an ancestor
    virtual void method();
    protected:
    virtual void m() = 0;
};

template<class X>
struct A1: public A<X>{  // goes to the file tf2.h to the class B1
    protected:
    void m();
};

template<class X>
struct A2: public A<X>{  // goes to the file tf2.h to the class B2
    protected:
    void m();
};

tf1.cpp

#include "tf1.h"


template<class X>
void A<X>::method(){
    /* here the class X used */
    std::cout << "A::method called" << std::endl;
    m();
}

template<class X>
void A1<X>::m(){
    std::cout << "A1::m called" << std::endl;
}

template<class X>
void A2<X>::m(){
    std::cout << "A1::m called" << std::endl;
}

tf2.h

#include "tf1.h"

class B{   // is the counterpain of the class _A
    protected:
    class BY: public Y{
    };

    _A * mp_x;
};


class B1: public B{  // is the counterpain of the class A1
    public:
    B1(){mp_x = new A1<BY>; std::cout << "B::B is called" << std::endl;}
};

class B2: public B{  // is the counterpain of the class A2
    public:
    B2(){mp_x = new A2<BY>; std::cout << "C::C is called" << std::endl;}
};

tfmain.cpp

#include <stdlib.h>

#include "tf2.h"

int main (int,char**){
    B2 b2;
    system ("PAUSE");
}

And, finally, the problem.

d:>g++ -std=c++0x tfmain.cpp -o tfmain.exe
ccB2BnSP.o:tfmain.cpp:(.rdata$_ZTV2A2IN1B2BYEE[__ZTV2A2IN1B2BYEE]+0x8): undefined reference to `A<B::BY>::method()'
ccB2BnSP.o:tfmain.cpp:(.rdata$_ZTV2A2IN1B2BYEE[__ZTV2A2IN1B2BYEE]+0xc): undefined reference to `A2<B::BY>::m()'
ccB2BnSP.o:tfmain.cpp:(.rdata$_ZTV1AIN1B2BYEE[__ZTV1AIN1B2BYEE]+0x8): undefined reference to `A<B::BY>::method()

I am using MinGW v4.7.2

  • 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-15T08:41:28+00:00Added an answer on June 15, 2026 at 8:41 am

    To put it simply, you can’t put template definitions into a cpp file (there are workarounds… but not pleasant ones)

    The reason is that the compiler only instantiates a template at the first point you invoke that template, since the compiler needs to know what type to instantiates the template with.

    Now, if you put the template definition into a separate cpp file which gets compiled separately into its own translation unit. Where does the compiler look for cases where you instantiate the template?

    For example

    // Template.h
    template <typename T>
    class templateObj { ~templateObj(); };
    
    // Template.cpp
    #include "Template.h"
    template <typename T>
    templateObj::~templateObj() { /* delete stuff! */ }
    
    // YourFile.cpp
    #include "Template.h"
    templateObj<int> myObj;
    

    Now, when the compiler compiles this code.
    It will generate two translation units, one for Template.cpp, the other for YourFile.cpp.

    Note that Template.cpp has not a single clue of what YourFile.cpp is and what’s inside it. So it has no way of knowning that in YourFile.cpp, you have used templateObj with a template parameter type int. Because of this, in the resulting translation unit of Template.cpp, the compiler will not generate an instantiated version of the destructor of templateObj

    Now, let’s look at YourFile.cpp, when the compiler sees that you’re instantiating templateObj with type int, it goes to look for the definition of templateObj. Since you have included Template.h, the compiler sees that you have declared a destructor for templateObj. But where is the definiton??

    The compiler doesnt see the definition of ~templateObj(), nor does it know where to look for at this point.
    So it simply holds off and will pass on to the linker to search for the correct module to link to.

    Now here is the problem:

    In the two translation units the compiler has just produced, neither YourFile.o or Template.o has the definition for template<int>::~template().
    The linker reads in YourFile.o and expects to have that version of the destructor of templateObj to link to, but the only other translation unit Template.o doesn’t have it; in fact, it has nothing.

    So what now? The linker has to complain… and that’s the error message you get.


    A bit more details about what happened with your code:

    • Two translation units are produced: tf1.o and tfmain.o
    • tf1.o is generated from tf1.cpp and whatever it includes.
    • tfmain.o is generated from tfmain.cpp and whatever itcludes.

    So what do they include? What does tf1.cpp and tfmain.cpp know about the rest of the code?

    tf1.cpp

    • #include "tf1.h" ==> which #include <iostream>…

    tfmain.cpp

    • #include "tf2.h" ==> which #include "tf1.h" ==> which #include <iostream>…

    What does tf1.cpp have? What does it know?

    • knows declarations of Y and _A
    • knows template declarations of A, A1 and A2
    • has template definitions for various methods in A, A1, and A2

    What does tfmain.cpp have? What does it know?

    • knows declarations of B, B1, B2, Y and _A
    • knows template declarations of A, A1 and A2
    • has definitions for various methods in B, B1, B2
    • has main and an instance of B2

    So, now the questions are:

    Does tf1.cpp know anything about tf2.h or tfmain.cpp? Does it know that you’re creating an instance to B2 which instantiates A2 with type BY?

    Does tfmain.cpp know anything about tf1.cpp? Does it know the definitions of methods in A, A1 or A2??

    They do NOT know each other, and thus the compiler has no way to generate the code for the definiton to your template classes in the translation unit tf1.o (At that point, it doesn’t even know you’re creating an instance to B2 which instantiates A2 with type BY).

    And finally the linker has no way to find the code tfmain.o is asking for, since it is NOT there.

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

Sidebar

Related Questions

Possible Duplicate: Why do I get “unresolved external symbol” errors when using templates? Why
Possible Duplicate: Why do I get “unresolved external symbol” errors when using templates? I
Possible Duplicate: Why do I get “unresolved external symbol” errors when using templates? “undefined
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: Get the resolution of a jpeg image using C# and the .NET

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.