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

  • Home
  • SEARCH
  • 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 6850717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:09:34+00:00 2026-05-27T01:09:34+00:00

I’ve been searching for a while and the closest thing to an answer was

  • 0

I’ve been searching for a while and the closest thing to an answer was over there

toString override in C++

However I was not able to make it work in my class.

I have a Table2D.h which contains this:

std::string toString() const;
std::ostream & operator<<( std::ostream & o, const Table2D<T> & s );

and I have a template class Table2D.template which contains this:

template <class T>
std::ostream & :: operator<<( std::ostream & o, const Table2D<T> & s ){
    return out << s.toString();
}

when I call my toString() function from main, it functions correctly.
However when I invoke the << operator using a std::cout I get the following errors.

Table2D.h(59): error C2804: binary 'operator <<' has too many parameters
Table2D.h(85) : see reference to class template instantiation 'Table2D<T>' being compiled
Table2D.template(100): error C2039: '<<' : is not a member of '`global namespace''
Table2D.h(59): error C2804: binary 'operator <<' has too many parameters

just so you know the 59’th line contains

for (unsigned y=0; y<m_height; y++) col_ptr[y] = (T) col_ptr_src[y];

which as you see contains no <<‘s so I’m not entirely certain what it’s referring to.


Edit:

After removing the declaration from the class, I replaced it’s entry in the header file with this

template <class T>
std::ostream& operator<<( std::ostream& o, const Table2D<T>& s ) {
    return o << s.toString();
}

and got the following error:

Table2D.h(60): error C2804: binary 'operator <<' has too many parameters
Table2D.h(89) : see reference to class template instantiation 'Table2D<T>' being compiled

the 89th line in the template file contains std::stringstream resultStream;

which is the very first line in my toString function, which looks like this

template <class T>
std::string Table2D<T> :: toString() const{
    std::stringstream resultStream;
    for(unsigned i = 0; i< m_height; i++){
        for (unsigned j = 0; j < m_width; j++){
            resultStream << (*this)[i][j] << "\t";
        }
        resultStream << endl;
    }
    return resultStream.str();
}
  • 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-27T01:09:35+00:00Added an answer on May 27, 2026 at 1:09 am

    Aside from your syntax being wrong1, operator<< overloads on other classes (ostream in this case) must be non-member functions. Change your definition to

    template <class T>
    std::ostream& operator<<( std::ostream& o, const Table2D<T>& s ) {
        return o << s.toString();
    }
    

    and remove its declaration from the class completely so that it is a free function.


    1 In case you want to know why, member-function binary operators only take one argument, because the left side is the invoking object, accessed via this. Also, you forgot the Table2D<T> before the :: in the definition. But even if you fixed these it wouldn’t work as intended because, as previously stated, operator overloading on other classes must be done through free functions.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.