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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:38:29+00:00 2026-06-02T21:38:29+00:00

I have to overload the insertion operator in order to view my class objects

  • 0

I have to overload the insertion operator in order to view my class objects in matrix format. I wrote the code but something’s wrong. When I include this to my code and trying to build, compiler gives me tons of errors; when I commented that part out, the errors are gone and program works correctly. Here is the code:

    template <class itemType> 
ostream & operator<< (ostream & os, const Storage2D<itemType> & rhs)
{
    node<itemType>* ptrRow = rhs.head;
    node<itemType>* ptrColumn = rhs.head;
    for(; ptrColumn->down != NULL; ptrColumn = ptrColumn->down)
    {
        ptrRow = ptrColumn;
        for(; ptrRow->right != NULL; ptrRow = ptrRow->right)
        {
            os << ptrRow->info << setw(10);
        }
        os << ptrRow->info << setw(10) << endl;
    }

    return os;
}

Here is how I tried to use overloading from main function:

Storage2D<double> m(row, column); 
cout << m;

It is not the member function of class Storage2D, it is written outside of the scope of class Storage2D in the implementation file.

It would be great if you help me, thanks in advance.

EDIT: Here is the rest of my code. The Storage2D.h file:

template <class itemType>
struct node
{
    itemType info;
    node* right;
    node* down;

    node()
    {}

    node(itemType data, node* r = NULL, node* d = NULL)
    {
        info = data;
        right = r;
        down = d;
    }
};

template <class itemType>
class Storage2D
{
public:
    Storage2D(const int & , const int & );      //constructor
    //~Storage2D();                             //destructor
    //Storage2D(const Storage2D & );                //deep copy constructor

private:
    node<itemType>* head;
};

ostream& operator<< (ostream & os, const Storage2D & rhs);

#include "Storage2D.cpp"
  • 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-02T21:38:35+00:00Added an answer on June 2, 2026 at 9:38 pm

    head is private so the operator needs to be a friend so it can access that data member. It also needs to be declared as a function template since Storage2D is a class template:

    #include <iostream> // for std::ostream
    
    template <class itemType>
    class storage2D {
    // as before
    template <typename T> 
    friend std::ostream& operator<< (std::ostream & os, const Storage2D<T> & rhs);
    };
    
    // declaration
    template <typename T> 
    std::ostream& operator<< (std::ostream & os, const Storage2D<T> & rhs);
    

    Note that I have explicitly used std::ostream, since ostream is in the std namespace.

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

Sidebar

Related Questions

I have the following code with the overload output operator in it: class Student
Possible Duplicate: Overload ++ operator Say i have a class in which i overloads
I have a Card class and I want to overload the > operator to
I overloaded the << operator of a class. How do I have to overload
Lets say I have a class A and I overload operator new in the
I have a class A that I overload its operator=. However it is required
I have a base Class akin to the code below. I'm attempting to overload
I have presented the code below. The compiler throws error when I overload an
Why is the Visual C++ compiler calling the wrong overload here? I am have
I have a ListActivty and I overload the onListItemClick(ListView listView, View view, int position,

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.