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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:34:08+00:00 2026-05-13T13:34:08+00:00

I’ve this program #include <iostream> #include <sstream> #include <iterator> #include <vector> #include <algorithm> using

  • 0

I’ve this program

#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std ;

#if 0
namespace skg 
{
 template <class T>
  struct Triplet ;
}

template <class T>
ostream& operator<< (ostream& os, const skg::Triplet<T>& p_t) ;
#endif

namespace skg
{
 template <class T>
  struct Triplet
  {
 //  friend ostream& ::operator<< <> (ostream& os, const Triplet<T>& p_t) ;

   private:
   T x, y, z ;

   public:
   Triplet (const T& p_x, const T& p_y, const T& p_z)
    : x(p_x), y(p_y), z(p_z) { }
  } ;
}

template <class T>
ostream& operator<< (ostream& os, const skg::Triplet<T>& p_t)
{
 os << '(' << p_t.x << ',' << p_t.y << ',' << p_t.z << ')' ;
 return os ;
}

namespace {
 void printVector()
 {
  typedef skg::Triplet<int> IntTriplet ;

  vector< IntTriplet > vti ;
  vti.push_back (IntTriplet (1, 2, 3)) ;
  vti.push_back (IntTriplet (5, 5, 66)) ;

  copy (vti.begin(), vti.end(), ostream_iterator<IntTriplet> (cout, "\n")) ;
 }
}
int main (void)
{
 printVector() ;
}

Compilation fails because compiler could not find any output operator for skg::Triplet. But output operator does exist.

If I move Triplet from skg namespace to global namespace everything works fine. what is wrong here ?

  • 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-13T13:34:09+00:00Added an answer on May 13, 2026 at 1:34 pm

    You need to move your implementation of operator<< into the same namespace as your class. It’s looking for:

    ostream& operator<< (ostream& os, const skg::Triplet<T>& p_t)
    

    But won’t find it because of a short-coming in argument-dependent look-up (ADL). ADL means that when you call a free function, it’ll look for that function in the namespaces of it’s arguments. This is the same reason we can do:

    std::cout << "Hello" << std::endl;
    

    Even though operator<<(std::ostream&, const char*) is in the std namespace. For your call, those namespaces are std and skg.

    It’s going to look in both, not find one in skg (since yours is in the global scope), then look in std. It will see possibilities (all the normal operator<<‘s), but none of those match. Because the code running (the code in ostream_iterator) is in the namespace std, access to the global namespace is completely gone.

    By placing your operator in the same namespace, ADL works. This is discussed in an article by Herb Sutter: “A Modest Proposal: Fixing ADL.”. (PDF). In fact, here’s a snippet from the article (demonstrating a shortcoming):

    // Example 2.4
    //
    // In some library header:
    //
    namespace N { class C {}; }
    int operator+( int i, N::C ) { return i+1; }
    
    // A mainline to exercise it:
    //
    #include <numeric>
    int main() {
        N::C a[10];
        std::accumulate( a, a+10, 0 ); // legal? not specified by the standard
    }
    

    Same situation you have.

    The book “C++ Coding Standards” by Sutter and & Alexandrescu has a useful guideline:

    1. Keep a type and its nonmember function interface in the same namespace.

    Follow it and you and ADL will be happy. I recommend this book, and even if you can’t get one at least read the PDF I linked above; it contains the relevant information you should need.


    Note that after you move the operator, you’ll need your friend directive (so you can access private variables):

    template <typename U>
    friend ostream& operator<< (ostream& os, const Triplet<U>& p_t);
    

    And ta-da! Fixed.

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

Sidebar

Related Questions

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
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.