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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:16:05+00:00 2026-06-03T16:16:05+00:00

Please consider the following code. I’m trying to output a vector of vectors to

  • 0

Please consider the following code. I’m trying to output a vector of vectors to an ostream.

#include <iterator>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

template<typename T>
std::ostream &operator <<(std::ostream &os, const std::vector<T> &v) {
    using namespace std;
    copy(v.begin(), v.end(), ostream_iterator<T>(os, "\n"));
    return os;
}

int main() {
    using namespace std;
    vector<string> v1;
    cout << v1;
    vector<vector<string> > v2;
    cout << v2;
    return 0;
}

The statement where I output a vector of strings works. The one where I output a vector of vectors of strings doesn’t. I’m using g++ 4.7.0. I’ve tried w/ & w/o the -std=c++11 flag. In C++11 mode, it gives me this line in the half-page of errors.

error: cannot bind 'std::ostream_iterator<std::vector<std::basic_string<char> >, char, std::char_traits<char> >::ostream_type {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

I don’t think I understand what it means. Could someone explain to me? I more or less know what an rvalue reference is, but I don’t see why std::basic_ostream<char> wouldn’t bind to std::basic_ostream<char>&&. Maybe I don’t know it well enough. And is there a better way to do this?

Thanks in advance.

  • 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-03T16:16:06+00:00Added an answer on June 3, 2026 at 4:16 pm

    The error you’re getting is a bit misleading. When I tried to compile your program I had to dig into the template vomit quite a bit, and I ended up with what I thought was going on:

    error: no match for 'operator<<' in '*((std::ostream_iterator<std::vector<std::basic_string<char> >, char, std::char_traits<char> >*)this)->std::ostream_iterator<std::vector<std::basic_string<char> >, char, std::char_traits<char> >::_M_stream << __value'
    

    Basically, when you called the copy algorithm, it used the output iterator, which used the << operator from within namespace std. Once there, lookup dictates that it try to find an overload for the template vector<> in the std namespace (because that’s where IT resides).

    So what you need to do is declare your stream operator for the vector template in namespace std. Surround your code with namespace std {} and see what happens…

    It should be noted that what you’re doing is basically modifying std::vector<> and adding behavior to it that wasn’t there before. Doing this is non-standard, undefined, and can easily get in your way. You might consider other options.


    I was wrong about this being a koenig lookup thing. It’s not, the issue is name hiding similar to what occurs in classes here you declare an overload of something in a base (not an override).

    The standard namespace declares several ‘<<‘ operators. These are basically functions named operator <<. In essence what you have is this:

    void fun(int);
    
    namespace Test {
    
      void fun() { fun(3); }
    
    }
    
    int main() {
        Test::fun();
    }
    

    Note that you can use fun(int) from the global namespace or any namespace that does not have any function named fun in it. You can’t use it from the Test namespace.

    This is why your use of operator << declared globally works fine from the global namespace but not from within the std namespace. The std namespace already has things named the same thing as the overload you’re trying to provide and so that overload is hidden from all things within std. If you could put a using declaration there things would be different.

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

Sidebar

Related Questions

Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Please consider the following code: NSString *string = @ä; const char *str1 = [string
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following code : string[] words = { Too, much, of, anything,
Please consider the following code: vector<int> myVector; myVector.push_back(10); myVector.erase(myVector.end()); This code compiles and runs
Please clarify my doubt in Hashset. Consider the following code, class Person { String
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Please consider the following code, struct foo { foo() { std::cout << Constructing! <<

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.