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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:09:52+00:00 2026-05-11T09:09:52+00:00

I have a class, defined in a head as: template <typename T> class MyClass

  • 0

I have a class, defined in a head as:

template <typename T> class MyClass {    template <typename U> friend std::ostream& operator<<(std::ostream& output, const MyClass<U>& p);    public:       ... } 

In an implementation file, I have:

template <typename U> std::ostream& operator<<(std::ostream& output, const MyClass<U>& m) {    output << 'Some stuff';    return output; } 

Which all looks fairly kosher. However, when I try and use this operator (i.e. std::cout << MyClass()), I get the following linker error:

Undefined symbols: std::basic_ostream<char, std::char_traits<char> >& operator<< <InnerType>(std::basic_ostream<char, std::char_traits<char> >&, MyClass<InnerType> const&) 

I am suprised the compiler hasn’t automagicially generated this for me… Any suggestions as to what I’m doing wrong?

  • 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. 2026-05-11T09:09:52+00:00Added an answer on May 11, 2026 at 9:09 am

    In an implementation file, I have:

    That’s the problem. You can’t split template definitions between header and implementation files. Due to the nature of templates, C++ compilers are finicky here. Define all the code in the header to make it work.

    In fact, the problem here is that all template definitions must reside within the same compilation unit because the C++ standard doesn’t define how template information are shared across different units. These units are stitched together by the linker, but generics are resolved at compile time (which is earlier), not at link time.

    Theoretically, the C++ standard defines a keyword, export, to handle these cases. In practice, no compiler implements this (with one exception?), and there is no intention to change this because the cost/usefulness trade-off is not considered good enough.

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

Sidebar

Related Questions

I have a templated class defined (in part) as template <class T> MyClass {
If I have the core of a class defined in one file as public
I have a class with two methods defined in it. public class Routines {
I have a private const int defined in a C# class. I want compilation
Suppose I have an array of a objects of user defined class. Wanted to
I have the following members defined in a class that I'm trying to deserialise:
Let's say we have defined a CSS class that is being applied to various
I have defined an interface in C++, i.e. a class containing only pure virtual
I have seen some C++ classes with a destructor defined as follows: class someClass
I have a few tables that I've defined like the below examples: class TableA

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.