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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:37:49+00:00 2026-05-26T09:37:49+00:00

Int this class where operator<< is defined (see code) while trying to compile it

  • 0

Int this class where operator<< is defined (see code) while trying to compile it with gcc 4.6.1 I’m getting following error: no match for ‘operator<<‘ in ‘std::cout << a’. What’s going on?

template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range = std::numeric_limits<Int_T>::min(),
                            typename Best_Fit<Int_T>::type Max_Range = std::numeric_limits<Int_T>::max()>
class Int
{
Int_T data_;  

Int_T get_data()const
{
return data_;
}

};  
//Here is this operator defined
template<class Int_T>
std::ostream& operator<<(std::ostream& out, const Int<Int_T, Best_Fit<Int_T>::type, Best_Fit<Int_T>::type>& obj)
{
    out << obj.get_data();
    return out;
}

where Best_Fit looks like:

#ifndef BEST_FIT_H_INCLUDED
#define BEST_FIT_H_INCLUDED


struct Signed_Type
{
    typedef long long type;
};

struct Unsigned_Type
{
    typedef unsigned long long type;
};

template<bool Cond, class First, class Second>
struct if_
{
    typedef typename First::type type;
};

template<class First, class Second>
struct if_<false,First,Second>
{
    typedef typename Second::type type;
};

template<class Int_T>
struct Best_Fit
{//evaluate it lazily ;)
    typedef typename if_<std::is_signed<Int_T>::value,Signed_Type,Unsigned_Type>::type type;
};

#endif // BEST_FIT_H_INCLUDED

edit:

#include <iostream>  
int main(int argc, char* argv[])
{
    Int<signed char,1,20> a(30);

    cout << a;
}
  • 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-26T09:37:50+00:00Added an answer on May 26, 2026 at 9:37 am

    Your template has three arguments, a type, and two constants of a known best fit type, but your templated operator<< takes an instantiation of the template with three types.

    template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range
                                         = std::numeric_limits<Int_T>::min(), // constant!
                                typename Best_Fit<Int_T>::type Max_Range
                                         = std::numeric_limits<Int_T>::max()  // constant!
            >
    class Int
    //...
    template<class Int_T>
    std::ostream& operator<<(std::ostream& out, 
                             const Int<Int_T, 
                                       Best_Fit<Int_T>::type, // type!
                                       Best_Fit<Int_T>::type  // type!
                             >& obj)
    

    I usually recommend that operator overloads of class templates are defined inside the class definition (use friend to define a free function in that context) for this particular reason, it is trivial to get the types right inside the class template, and easy to fail outside of it. There are a couple other differences (like the fact that if the operator is defined inside the class then it will only be accessible through ADL –unless you also decide to declare it outside)

    template<class Int_T = int, typename Best_Fit<Int_T>::type Min_Range
                                         = std::numeric_limits<Int_T>::min(), // constant!
                                typename Best_Fit<Int_T>::type Max_Range
                                         = std::numeric_limits<Int_T>::max()  // constant!
            >
    class Int {
       friend                  // allows you to define a free function inside the class
       std::ostream& operator<<( std::ostream& out, 
                                 Int const & obj ) {  // Can use plain Int to refer to this 
                                                      // intantiation. No need to redeclare
                                                      // all template arguments
           return out << obj.get_data();
       }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
From time to time I see something like this: class Clazz { private int
the following code compiles fine under gcc: class vec3 { private: float data[3]; public:
In trying to compile this program: namespace MyNamespace { template<typename T> class Test {
Given this class: class C { private: struct Foo { int key1, key2, value;
Take this class as example: public class Category : PersistentObject<int> { public virtual string
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
Say I have this class: class myclass { public int Field1{ get; set; }
Say I have this class: public class Account { public int AccountID { get;
Say I've got a class like this: class Test { int x; SomeClass s;

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.