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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:43:15+00:00 2026-05-28T02:43:15+00:00

Possible Duplicate: Why can templates only be implemented in the header file? I’ve been

  • 0

Possible Duplicate:
Why can templates only be implemented in the header file?

I’ve been trying around with C++ recently.
At the moment I’m trying to program something I’m sure everone has done at least once: A simple LinkedList class.
The code is done, but I’m somehow failing to compile it. I’ve been googling and it seems like I’m linking the object files wrong. That’s what my code basically looks like:

test.cpp

#include "linkedlist.h"

int main()
{
    LinkedList<int> list;
    // do something
}

linkedlist.h

template <typename T>
class LinkedList
{
   // a lot of function and variable definitions
}

Then there’s a .cpp file called linkedlist.cpp which contains all the actual code of the LinkerList class. When trying to compile test.cpp using the following command:

g++ ..\src\test.cpp

I’m getting told that there’s an undefined reference to ‘LinkedList::LinkedList()’. So I’ve been thinking that it’s being linked wrong as there’s more than one .cpp file, so I tried it like this:

g++ -c -Wall -O2 ..\src\test.cpp
g++ -c -Wall -O2 ..\src\linkedlist.cpp
g++ -s test.o linkedlist.o

However, this doesn’t change anything. The error messages stay the same.
I’ve been trying to find some information on the internet, however, it didn’t really work out.

  • 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-28T02:43:15+00:00Added an answer on May 28, 2026 at 2:43 am

    You’re creating a class template, which has the important caveat that all variable definitions must be placed in the header file so that they’re accessible to all translation units during compilation.

    The reason is the way that templates work. During compilation, the compiler instantiates template classes based on your class template definition. It isn’t enough for it to have access to only the declarations or signatures: it needs to have the entire definition available.

    Move all of your method definitions out of the .cpp file and into the .h file, and everything should be fine (assuming that you have, in fact, provided a definition for the default constructor!).

    Alternatively, you might be able to get around this by explicitly telling your compiler to instantiate the appropriate template class using something like template class LinkedList<int>, but this really isn’t necessary in such a simple case. The primary advantage of this over including all of the definitions in the header file is that it potentially reduces code bloat and speeds up compilation. But it might not be necessary at all, as compilers have gotten a lot smarter at applying appropriate optimizations.

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

Sidebar

Related Questions

Possible Duplicate: Why can templates only be implemented in the header file? When I
Possible Duplicate: Why can templates only be implemented in the header file? Why should
Possible Duplicate: Why can templates only be implemented in the header file? Undefined reference
Possible Duplicate: Why can templates only be implemented in the header file? I've struggled
Possible Duplicate: Why can templates only be implemented in the header file? hello everyone
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: Use templates to get an array's size and end address Can someone
Possible Duplicate: C++ templates that accept only certain types For example, if we want
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got

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.