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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:05:55+00:00 2026-06-13T13:05:55+00:00

I have this function declaration: template<class T> a::A& a::A::operator<<(T out) { std::cout << out;

  • 0

I have this function declaration:

template<class T>
a::A& a::A::operator<<(T out) {
    std::cout << out;
    return (*this);
}

and this function definition:

namespace a {
    ...
    class A {
        ...
        template<class T> A& operator<<(T);

And I call it as:

a::A b;
b << 1;

and this is the Makefile:

app: main.o A.o
    g++ main.o A.o -o app

main.o: main.cpp
    g++ -c main.cpp

A.o: A.cpp
    g++ -c A.cpp

and it gives me:

Undefined symbols: a::A& a::A::operator<< <int>(int)

why is that?

  • 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-06-13T13:05:57+00:00Added an answer on June 13, 2026 at 1:05 pm

    The function template will be turned into an actual function at compile time, once the type represented by T (that is, int in your case) is actually known. However, this is not the case before main.cpp is compiled. At the time when A.cpp is compiled, the template function is not instantiated into an actual function, therefore the object file generated doesn’t include the binary version of the function.

    There are two ways to solve this.

    1. Include the function definition in your header file. That is, make

      template<class T>
      a::A& a::A::operator<<(T out) {
          std::cout << out;
          return (*this);
      }
      

      a part of the header file, and remove the function definition from the .cpp file.

      The effect of this is that any .cpp file that includes this header will be able to use any instantiation of the template, i.e. for any value of T.

    2. Alternatively, include an explicit template instantiation statement in A.cpp:

      template a::A& a::A::operator<<(int out);
      

      This will cause the compiler to actually instantiate the template when A.cpp is compiled, and to include the compiled function in the object file. Hence the linker can find it when linking main.o and A.o together, and all is fine. The disadvantage is that it will only work for the specific types (in this case, only int) that you provided explicit instantiations for.

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

Sidebar

Related Questions

I have a function B that has this declaration: public void B(Class<? extends C>
I have this function: int firstHeapArray (IntHeapArray h) { if(!emptyHeapArray(h)) return h.array[0]; } It's
I have this function: function validate($data) { $newData = str_replace(&nbsp;, , $newData); $newData =
template<typename T> class Base { protected: Base() {} T& get() { return t; }
function definition const circularLinkedList<Tp>& operator=(const circularLinkedList<Tp>& otherList); lines that cause the error, error message
I have this setup: class DontUse; template<class T,class U = DontUse, class V =
I have a class template and I can't seem to figure out how to
In trying to compile this program: namespace MyNamespace { template<typename T> class Test {
C++0x shows an example of using std::forward : template<class T> void foo(T&& arg) {
I have a class template Foo<T> . I'd like to implement a non-member 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.