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 3274088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:00:02+00:00 2026-05-17T19:00:02+00:00

I have created an example below to illustrate the problem I’m having. Basically, when

  • 0

I have created an example below to illustrate the problem I’m having. Basically, when I separate a template class into separate .h/.cpp file, I get unresolved symbols for the constructor. Using a single file, it compiles fine. What is causing this?

fruits.cpp:

#include "apple.h"

class FruitsDB {
    public:
        void addApple();
};

void FruitsDB::addApple() {
    Apple<int> m;
}

int main() {
    FruitsDB fruits;
    return 0;
}

apple.h:

template <typename T>
class Apple {
    public:
        Apple();
        ~Apple();
};

apple.cpp

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

template <typename T>
Apple<T>::~Apple() {
}

This produces the compiler error:

g++    -c -o fruits.o fruits.cpp
g++    -c -o apple.o apple.cpp
g++ -Wall -ggdb fruits.o apple.o -o fruits
Undefined symbols:
  "Apple<int>::Apple()", referenced from:
      FruitsDB::addApple()     in fruits.o
  "Apple<int>::~Apple()", referenced from:
      FruitsDB::addApple()     in fruits.o
ld: symbol(s) not found

I thought it was my code causing the problem, but consolidating files doesn’t produce the problem. I’m assuming I need to include a file somewhere where I’m not. I’m totally lost.

Note: Adding template <> Apple<int>::Apple() {} in apple.cpp will resolve this, but I’m using templates to avoid having to write all those constructors, etc.

  • 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-05-17T19:00:02+00:00Added an answer on May 17, 2026 at 7:00 pm

    Just put your template function definitions in your header files. The thing about template functions is, they aren’t actually functions. They are templates of functions. So they can’t be compiled out of context like normal functions. They can only be compiled if the template parameters are known. So the compiling is done when the file that uses them calls the function. In order for that to happen, the template needs to be visible to the file from which it is called, which is why you put it in the header.

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

Sidebar

Related Questions

I have created a separate class (let's call it class2.cs for example) and want
I have created the following example to illustrate a problem I have with a
I have created a little example(below) on my attempt to safely stop multiple threads
I have created an exe with some methods in it (ref example exe below).
I have created a benchmark class that allows the user to insert for example
I have created an inherited document class, see the code below. The documents persists
EDIT: My example might have created some confusion. I have changed the example below
I have created the jQuery Script below, But am having a few problems converting
I want to create an example like the image below but instead have a
I have created a little example,please have a look, http://jsfiddle.net/bWTwL/ I want to have

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.