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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:02:28+00:00 2026-05-30T05:02:28+00:00

Okay I’m a little stuck on trying to overload the << operator for my

  • 0

Okay I’m a little stuck on trying to overload the << operator for my template class. The requirement is that the << operator must call a void print function defined for this class.

Here is the important stuff from the template header:

template <class T>
class MyTemp {
public:
    MyTemp();           //constructor

    friend std::ostream& operator<< (std::ostream& os, const MyTemp<T>& a);

    void print(std::ostream& os, char ofc = ' ') const;

and here is my print function basically it’s a vector and prints last element to first:

    template <class T>
void Stack<T>::print(std::ostream& os, char ofc = ' ') const
{
    for ( int i = (fixstack.size()-1); i >= 0 ; --i)
    {
        os << fixstack[i] << ofc;
    }
}

and here is how I have the operator<< overloaded:

    template <class T>
std::ostream& operator<< (std::ostream& os, const Stack<T>& a)
{
    // So here I need to call the a.print() function
}

But I am receiving an “unresolved external symbol” error. So really I guess I have two issues. The first, is the way to fix the error above. Second, once that is fixed would I just call a.print(os) inside << overload? I know it needs to return an ostream though. Any help would be greatly appreciated!

  • 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-30T05:02:29+00:00Added an answer on May 30, 2026 at 5:02 am

    The simplest thing to do would be to leave print public (as it is in your example), so the operator doesn’t need to be a friend.

    template <class T>
    class MyTemp {
    public:
        void print(std::ostream& os, char ofc = ' ') const;
    };
    
    template <class T>
    std::ostream& operator<< (std::ostream& os, const MyTemp<T>& a) {
        a.print(os);
        return os;
    }
    

    If you do need it to be private, then you need to declare the correct template specialisation to be a friend – your friend declaration declares a non-template operator in the surrounding namespace, not a template. Unfortunately, to make a template a friend you need to declare it beforehand:

    // Declare the templates first
    template <class T> class MyTemp;
    template <class T> std::ostream& operator<< (std::ostream&, const MyTemp<T>&);
    
    template <class T>
    class MyTemp {
    public:
        friend std::ostream& operator<< <>(std::ostream& os, const MyTemp<T>& a);
        // With a template thingy here  ^^
    
    private:
        void print(std::ostream& os, char ofc = ' ') const;
    };
    
    template <class T>
    std::ostream& operator<< (std::ostream& os, const MyTemp<T>& a) {
        a.print(os);
        return os;
    }
    

    Or you could define the operator inline:

    template <class T>
    class MyTemp {
    public:
        friend std::ostream& operator<<(std::ostream& os, const MyTemp<T>& a) {
            a.print(os);
            return os;
        }
    
    private:
        void print(std::ostream& os, char ofc = ' ') const;
    };
    

    For your last question:

    Second, once that is fixed would I just call a.print(os) inside << overload? I know it needs to return an ostream though.

    It does indeed need to return an ostream – so just return the one that was passed in, as in my example code.

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

Sidebar

Related Questions

Okay, so I'm trying to make a game that uses this algorithm: http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection But
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay, so I'm working on a java server for an Apps backend, it must
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay so the title may be a bit misleading. What I am trying to
Okay, I'm trying to reverse engineer a feature on a website I found -
Okay, I'm a little fuzzy on how this works or if it's possible. I
Okay so here is what I'm trying to accomplish. First of all below table
Okay, so I'm trying to create a Order Posts by Type using jQuery JSON
Okay, I'm trying to make a div fit to the size of the browser

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.