Solved

No match for operator "<<"

Posted on 2010-11-10
4
790 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ finding a sting in a char* string from a text file 3 120
GUI: DIalog Stacking and Popping in MS C++ 4 82
White board coding practice 3 92
C++ help/ Toy problem 19 30
  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 …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.

830 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