Solved

No match for operator "<<"

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now