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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:09:59+00:00 2026-06-13T04:09:59+00:00

I have a typedef inside a class and I would like to overload the

  • 0

I have a typedef inside a class and I would like to overload the operator<< for it to be able to print it in ostream. However, the compiler cannot find the overloaded operator. How can I declare it so that it works?

#include <iostream>
#include <set>

using namespace std;

template <class C>
struct K {

    typedef std::set<C> Cset;

    Cset s;
    // and many more elements here

    friend ostream& operator<<(ostream& oo, const Cset& ss){
        typename Cset::const_iterator it=ss.begin();
        oo << "[";
        for(; it!=ss.end(); ++it) oo << (*it) << ",";
        oo << "]";
        return oo;
    }

    void DoSomething(){
        // do something complicated here
        cout << s << endl;
        // do something complicated here
    }

};


int main(){
    K <int> k;
    k.s.insert(5);
    k.s.insert(3);
    k.DoSomething();

}

gcc version 4.4.5 20101112 (Red Hat 4.4.5-2) (GCC)

  • 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-13T04:10:00+00:00Added an answer on June 13, 2026 at 4:10 am

    When a friend function is defined inline and there is no forward declaration outside the class, it is only found by ADL. However, your overload will never be found by ADL as it does not involve K arguments (note that K<int>::CSet is a typedef for std::set<C>).

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

Sidebar

Related Questions

I have typedef std::string OrderID; I would like to overload the operator ++ for
I have a typedef inside a class from a c++ machine library (dlib) that
I have this bit of code: typedef CComQIPtr<MSXML::IXMLDOMDocument2> XML_DocumentPtr; then inside some class: XML_DocumentPtr
Say inside of a class Card , you have declared typedef enum { CLUBS,
I have a class with a few simple structs like this (simplified code) inside
I have a metafunction: struct METAFUNCION { template<class T> struct apply { typedef T
I have a class that represents an capture zone in a game. Inside of
Sorry for the long title. I have a typedef in a class List: template
I have a template class, inside which i have a normal function. But i
I'm trying to make a template class where one would be able to define

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.