Boost logo

Boost Users :

Subject: Re: [Boost-users] [BGL] Encountering "bad lexical cast" exception when reading a class from graphviz file
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2012-07-21 14:43:03


On Sat, 21 Jul 2012, Nicholas Mario Wardhana wrote:

> Hi all,
>
> I am trying to read a graphviz file into my graph implementation. The
> minimum working code as well as the graphviz file are at the end of
> this e-mail. The node of the graph is the Vertex struct, which
> contains a point object (Point p). For Point class, I overload two
> operators, >> and <<, and operator << was previously used to generate
> the graphviz file. The operator overloads work well with std::cin and
> std::cout, but with the code below, I receive an exception:
>
> bad lexical cast: source type value could not be interpreted as target .
>
> The culprit might be in the << overload. In particular, when reading
> from a file, the value of iToBool1 is true, whereas iToBool2 is false.
> That is to say, the variable i might change when inserting the stream
> data to the point. This does not happen when reading from std::cin, as
> both variables are true.
>
> I convert the i variable, originally an istream, to bool because when
> the program returns to the calling function, which is
> lexical_stream_limited_src::shr_using_base_class on line 1616 in
> lexical_cast.hpp, this functions uses the return value of << operator
> in a boolean comparison. The value of iToBool2 being false eventually
> leads to calling BOOST_LCAST_THROW_BAD_CAST on line 1975.
>
> I wonder if somebody can tell me what is wrong with my operator overload?
>
> I use Boost 1.50 and Visual Studio 2008 on Windows 7.

Can you do lexical_cast<Point>("1 2 3")? If that fails, make sure that
reading that string as a Point using istringstream reads the entire
contents of the stream (hits EOF and does not set failbit); I am not good
enough with streams to know exactly why your code is failing beyond that.
If the lexical_cast succeeds, there is a bug in BGL's parsing of the
string and I'll take a look at it further.

-- Jeremiah Willcock


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net