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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:00:06+00:00 2026-05-20T12:00:06+00:00

I have a template class A, and a stream operator function for A that

  • 0
  • I have a template class A, and a stream operator function for A that is consequently a template function.
  • I have a class B that inherit from A, specifying a type for the template and adding some stuff specific to B.
  • I am writing a stream operator on B that does something specific to B and then falls back to the rest of the stream operations programmed for A.

Because B is a subclass of A (do you call it that?), invoking A’s stream operator should work. In fact, operator>>(f,a) in main() works. But for some reason it doesn’t work on b casted to A. The error I get is “no matching function for call to ‘operator>>(std::basic_istream >&, A)”.

What am I doing wrong?

Here is the sample code:

#include <stdlib.h>
#include <fstream>
#include <iostream>

using namespace std;


template <class TypeT>
class A
{
public:
    A(){a = -9;}
    A(TypeT v){a = v;}
    TypeT a;
};

class B : public A<int>
{
public:
    B(int w) : A<int>(10) {b = w;}
    int b;
};

template <class TypeT>
istream &operator>> (istream &s, A<TypeT> &a)
{
    cout << "a.a = " << a.a << endl;
    return s;
}

istream &operator>> (istream &s, B &b)
{
    cout << "b.b = " << b.b << "  ";
    operator>>( s, (A<int>)b);    // error!
    return s;
}



int main(void) {
    ifstream f("/dev/null");

    A<int> a(0);
    operator>>( f, a );

    B b(1);
    operator>>( f, b );

    return EXIT_SUCCESS;
}

.

  • 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-20T12:00:07+00:00Added an answer on May 20, 2026 at 12:00 pm

    Change your cast to:

    operator>>(s, (A<int> &)b);
    

    Your original cast creates a temporary which you can only get a const reference to.

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

Sidebar

Related Questions

I have written a function template for serialization of enums to/from our stream class
I have a template class with a variadic template member function that I am
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I want to have a template class that looks something like what I have
I'm converting my fields class read functions into one template function. I have field
I have a function template as this. template<class T> T getFromString(const string& inStream) {
I have two functions that I have overloaded for my debug class: template<class IteratorT>
I have a template class that I have made called hash . My template
I have a class for logging, which MUST NOT inherit std::ostream, and has operator<<
I have a class which has some methods like follows (And more): template<class T>

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.