Solved

No match for operator "<<"

Posted on 2010-11-10
4
799 Views
Last Modified: 2012-05-10
Hello,
Im trying to compile boost's variant example which is given in boost web site.

 
#include "boost/variant.hpp"
#include <iostream>

class my_visitor : public boost::static_visitor<int>
{
public:
    int operator()(int i) const
    {
        return i;
    }
    
    int operator()(const std::string & str) const
    {
        return str.length();
    }
};

int main()
{
    boost::variant< int, std::string > u("hello world");
    std::cout << u; // output: hello world

    int result = boost::apply_visitor( my_visitor(), u );
    std::cout << result; // output: 11 (i.e., length of "hello world")
}

Open in new window


But it gives an error like this

No match for operator "<<" ...    ...   ....     variant_io.hpp

What could be the problem?

Kind regards
0
Comment
Question by:vileda
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 12

Expert Comment

by:HappyCactus
ID: 34109690
Here it compiles and work fine:

HappyCactus$ g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

anaconda:b HappyCactus$ grep BOOST_LIB_VERSION /usr/local/include/boost/version.hpp
//  BOOST_LIB_VERSION must be defined to be the same as BOOST_VERSION
#define BOOST_LIB_VERSION "1_44"
anaconda:b HappyCactus$

Check carefully your compilation options and the include path. If you have multiple version of boost, remove all but the most recent. and upgrade boost.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 400 total points
ID: 34109751
I too was able to compile it with gcc but not with Visual Studio. The code below makes it work. I suspect this is an issue in the Windows version of Variant or Visual Studio being useless.
#include "boost/variant.hpp"
#include <iostream>
#include <string>

class my_visitor : public boost::static_visitor<int>
{
public:
   int operator()(int i) const
   {
      return i;
   }

   int operator()(const std::string & str) const
   {
      return str.length();
   }

};

class ostream_visitor : public boost::static_visitor<std::ostream &>
{
public:
   ostream_visitor(std::ostream & out) : out(out)
   {
   }

   template <typename T>
   std::ostream & operator()(T const & t) const
   {
      return out << t;
   }

private:
   std::ostream & out;
};

std::ostream & operator << (std::ostream & out, boost::variant< int, std::string > const & u)
{
   return boost::apply_visitor( ostream_visitor(out), u );
}

int main()
{
   boost::variant< int, std::string > u("hello world");
   std::cout << u << std::endl; // output: hello world

   int result = boost::apply_visitor( my_visitor(), u );
   std::cout << result; // output: 11 (i.e., length of "hello world")
}

Open in new window

0
 

Author Comment

by:vileda
ID: 34110698
I'm trying to compile it from QNX Momentics Windows version. The include path seems ok and I have only one version.
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 100 total points
ID: 34112065
OMG... the number of errors I got when I tried to compile the sample using Visual Studio 2010.

However, I too got the same error about '<<' and could quickly realize that the problem (as Visual Studio sees it) is that 'ostream' didn't have the operator '<<' define for the template.  Evilrix's example seems to supply that missing operator.

In other words, under Visual Studio 2010, the "Hello World" program listed in the original post will not compile, but Evilrix's version with the added operator definisions does.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 318
Better understanding on C++ Class serialization and formats 9 106
operator overload: incompatible type and too many params 5 99
max float value 3 76
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question