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 class defined as public class viewGroups extends ListActivity Somewhere in the
I have a class defined as: class Obj { public: int width, height; Obj(int
I have a template class defined like so: template <class T> class Command {
I have a class that defined a user defined operator for a TCHAR*, like
I have a Customer class defined like this: @Entity public class Customer { //...
Bit of a noob question but: I have a custom class defined as: public
I have a user defined class that I want to create a public List
I have a class defined like this, with the appropriate getter and setter methods...
We have a class Car defined like this in a car.rb file class Car
I have a class with defined functions that need to be passed as parameters.

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.