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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:37:34+00:00 2026-05-26T19:37:34+00:00

Take this code, for example: /* * foo.h * * Created on: Nov 5,

  • 0

Take this code, for example:

/*
 * foo.h
 *
 *  Created on: Nov 5, 2011
 *      Author: AutoBotAM
 */

#ifndef FOO_H_
#define FOO_H_

template<typename Type>
class Foo
{
public:
    void Bar(Type object);
};


#endif /* FOO_H_ */

.

/*
 * foo.cpp
 *
 *  Created on: Nov 5, 2011
 *      Author: AutoBotAM
 */

#include <iostream>
using namespace std;

#include "foo.h"

template<typename Type>
void Foo<Type>::Bar(Type object)
{
    cout << object;
}

.

/*
 * main.cpp
 *
 *  Created on: Oct 15, 2011
 *      Author: AutoBotAM
 */

#include <iostream>
using namespace std;

#include "foo.h"

Foo<int> foo;

int main()
{
    cout << "The year is ";
    foo.Bar(2011);
    return 0;
}

This is how I usually go about declaring non-template classes. Unfortunately this code results in the error ../src/main.cpp:18: undefined reference to 'Foo<int>::Bar(int)' (in MinGW). I did some reading, and it turns out you have to declare template classes in the same translation unit, like so:

/*
 * foo.h
 *
 *  Created on: Nov 5, 2011
 *      Author: AutoBotAM
 */

#ifndef FOO_H_
#define FOO_H_

template<typename Type>
class Foo
{
public:
    void Bar(Type object)
    {
        cout << object;
    }
};


#endif /* FOO_H_ */

My big question is, why do you have to do this? I could imagine a few pitfalls in development with this scheme. For example, imagine we had 50 translation units #including foo.h, and we make a change to void Foo::Bar(Type). Since Bar is in the header file, we’ll have to wait for all 50 of those translation units to compile before we get any results. If we had Bar working in foo.cpp separately, we would only have to wait for 1 translation unit to compile. Are there any ways to overcome this issue?

Thanks for any advice!

  • 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-26T19:37:35+00:00Added an answer on May 26, 2026 at 7:37 pm

    Templates are not types. They are only templates. They only become a type when they are instantiated (with a complete set of parameters).

    Compilation requires that all necessary type definitions be available at compile time. Moreover, linking requires that all necessary function definitions, including member functions, exist in some translation unit (with inlining providing the usual exemption to the one-definition-rule).

    If you put all this together, it follows almost automatically that all template member function definition of a template class must be available for every used template instantiation at some point in the compilation process.

    On the other hand, consider the following setup:

    // Header file:
    template <typename T> struct Foo { void f(); }
    
    // "Implementation" file
    template <typename T> void Foo::f() { /* stuff */ }
    

    If you compile the implementation file, it doesn’t contain any code, since there is no template instance to be compiled. A user of the header file may instantiate Foo<int>, but the body of the class never gets instantiated in any TU, so you get a linker error when piecing together the program.

    It may help to think of templates as a code generation tool rather than actual code, at least for the purpose of compilation.

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

Sidebar

Related Questions

Take this example code (ignore it being horribly inefficient for the moment) let listToString
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
The questions says everything, take this example code: <ul id=css-id> <li> <something:CustomControl ID=SomeThingElse runat=server
take a look at this example code: public class Comment { private Comment() {
take this simple code: class A{ public: virtual void foo() = 0; void x(){
Take the following code example: File package1/__init__.py : from moduleB import foo print moduleB.__name__
Take the following code in C/C++, for example: int foo[] = {0, 0, 0,
Take this code snippet as example: union stack { int a; float b; };
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
Take this code into consideration: Proxy p = new Proxy(Type.SOCKS, new InetSocketAddress(proxyURL, port)); try

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.