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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:07:38+00:00 2026-05-15T21:07:38+00:00

This problem is in defining and declaring a function template in a namespace that

  • 0

This problem is in defining and declaring a function template in a namespace that is defined in an external file from where the function is instantiated. Here’s the smallest reproducible example I could come up with. 4 files follow:

The function template declaration in a named namespace:

// bar.h
#include <algorithm>

namespace barspace {

  template <typename Iter>
    void DoSomething (Iter first, Iter last);

}

The function template definition in a separate file:

// bar.cpp
#include "bar.h"

namespace barspace {

  template <typename Iter>
    void DoSomething (Iter first, Iter last) {
      typedef typename std::iterator_traits<Iter>::value_type val_t;
      std::sort (first, last);
    }

} // namespace barspace

The header for the main program

// foo.h
#include "bar.h"
#include <vector>

Lastly, the main program where the function template is called:

//foo.cpp
#include "foo.h"

int main () {

  std::vector<double> v_d;
  for (int i = 0; i < 10; i++) {
    v_d.push_back (i);
  }

  barspace::DoSomething (v_d.begin(), v_d.end());

  return 0;
}

I compile as follows:

g++ -c -o bar.o bar.cpp

g++ -c -o foo.o foo.cpp

These run fine. Now for linking:

g++ bar.o foo.o -o foobar

And the resulting compiler error about the undefined reference:

foo.o: In function `main':
foo.cpp:(.text+0x6e): undefined reference to `void barspace::DoSomething<__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > > >(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >)'
collect2: ld returned 1 exit status

There is an obvious problem with the code not getting made available from within the namespace, or from the bar compilation unit.

Furthermore, when I try to place the definition of DoSomething in the bar.h header, as I would to circumvent the issues when defining class template methods in separate .cpp files, I get the same error.

Can you shed some light unto my compiler linking error?

  • 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-15T21:07:39+00:00Added an answer on May 15, 2026 at 9:07 pm

    You are trying to hide the implementation of your templated function into the cpp file, which, unfortunately, is not possible for most compilers. Templated functions/classes are instantiated when used, so at the point where you are calling DoSomething, the compiler needs the definition of the function to be able to compile it.

    There are a couple of solutions.

    1. Move the function body into the header file. You had trouble doing that before, but i’d say it’s related to something else. This is the preferred approach.

    2. Include the cpp file from foo.cpp. (wild, but not that uncommon).

    3. Instantiate the template for double:

    // bar.cpp
    #include "bar.h"
    
    namespace barspace {
    
      template<>
        void DoSomething<double> (double first, double last) {
          typedef typename std::iterator_traits<double>::value_type val_t;
          std::sort (first, last);
        }
    
    } // namespace barspace
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem. I defined a function to remove a link (just for
This problem is same as asked in here . Given a list of coins,
This problem is baffling me: BEGIN; INSERT INTO sub_users(user_id, email) SELECT user_id FROM users
Here is my problem: in a header I define a structure template type_to_string ,
I am facing this problem that buttons in android are not clickable when given
I previously tried breaking this problem down into smaller, simpler problems here and here
I have this annoying problem with padding. I am building a menu, here is
I am having problem defining CSS rules for browsers that do not support font-stretch
it seems that I have a problem with defining Long variables in routes .
I am a newbie in Haskell and have some problem of defining a function

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.