Solved

No match for operator "<<"

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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