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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:01:18+00:00 2026-05-22T20:01:18+00:00

Possible Duplicates: Templated function being reported as “undefined reference” during compilation Why can templates

  • 0

Possible Duplicates:
Templated function being reported as “undefined reference” during compilation
Why can templates only be implemented in the header file?

Hi,
I’m having a problem with dependencies in a c++ program that only originates when I use templates. Apparently the compiler is not able to instantiate the template for a concrete type and I don’t know why.

Suppose I have this library (the real program where I find this is much bigger, but I think this seems to reproduce my original problem):

//temp.cpp file
#include "temp.hpp"

template <typename a> 
void printVector(std::vector<a> xs) {
  for_each(xs.begin(), xs.end(), [](a x) {std::cout << x << " ";});
}

void printDoubVector(std::vector<double> xs) {
  for_each(xs.begin(), xs.end(), [](double x) {std::cout << x << " ";});
}

with this header file:

#include <vector>
#include <algorithm>
#include <iostream>


template <typename a> void printVector(std::vector<a> xs);
void printDoubVector(std::vector<double> xs);

And this main file:

#include "temp.hpp"

int main(){
  std::vector<double> foo(10,1);
  printVector(foo);
  std::cout << std::endl;
}

When I compile them with this makefile:

CC=g++-4.5
FLAGS = -std=gnu++0x -Wall

all: test

test: temp.o main.o
    $(CC) $(FLAGS) $^ -o $@

%.o: %.cpp
    $(CC) $(FLAGS) -c $^ -o $@

I got a undefined reference to 'void printVector<double>(std::vector<double, std::allocator<double> >)' error.

Of course, if I do printDoubVector(foo); instead of printVector(foo);, it compiles normally. If everything’s in the same file, it also compiles normally. I also tried to do printVector<double>(foo); with no result. Apparently the compiler knows I want a printVector<double>, he just can’t find it. 🙁

I don’t understand this problem. I was using templates in different files before without any problems at compilation. 🙁 Yet, it feels like something very basic that I should know at this level. :/

  • 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-22T20:01:19+00:00Added an answer on May 22, 2026 at 8:01 pm

    The reason is described in detail here.

    In short, you need to provide the full definition in the header-file, otherwise the compiler cannot instantiate the template.

    • 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? hello everyone
Possible Duplicate: How does this “size of array” template function work? Is there any
Possible Duplicate: Templates: template function not playing well with class’s template member function template
Possible Duplicates: [.NET] How do I disable a system device? Win32 API function to
Possible Duplicates: Function overloading by return type? Puzzle: Overload a C++ function according to
Possible Duplicate: Use templates to get an array's size and end address Can someone
Possible Duplicate: Where and why do I have to put “template” and “typename” on
Possible Duplicates: How can I convert ereg expressions to preg in PHP? What is
Possible Duplicates: C++0X when? When will C++0x be finished? When will C++0x be released?
Possible Duplicate: How to use local classes with templates? g++ 4.4 is refusing to

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.