Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

No match for operator "<<"

Posted on 2010-11-10
4
Medium Priority
?
862 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
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 1600 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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

885 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