|
Boost Users : |
Subject: [Boost-users] [BGL] Encountering "bad lexical cast" exception when reading a class from graphviz file
From: Nicholas Mario Wardhana (mario.wardhana_at_[hidden])
Date: 2012-07-21 07:19:23
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.
Thanks!
Best regards,
Nicholas
//--------------------Graphviz--------------------
graph G {
1 [Point="1 2 3"];
}
//----------------------code----------------------
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/graph/graphviz.hpp>
#include <iostream>
#include <fstream>
#include <string>
struct Point
{
float x;
float y;
float z;
inline friend std::istream& operator >>
( std::istream& i, Point& p )
{
bool iToBool1 = i;
i >> p.x >> p.y >> p.z;
bool iToBool2 = i;
return i;
}
inline friend std::ostream& operator <<
( std::ostream& o, const Point& p )
{
o << p.x << " " << p.y << " " << p.z;
return o;
}
};
struct Vertex
{
int ID;
Point p;
};
struct Edge
{
int ID;
};
typedef boost::adjacency_list<
boost::listS,
boost::vecS,
boost::undirectedS,
Vertex, // bundled data
Edge, // bundled data
boost::no_property, // not needed
boost::listS> Graph;
int main()
{
// Part 1: input via std::cin
Point p;
std::cin >> p;
Graph graph;
// Part 2: input from graphviz
boost::dynamic_properties dynamicProperties;
dynamicProperties.property("NodeID", boost::get(&Vertex::ID, graph));
dynamicProperties.property("Point", boost::get(&Vertex::p, graph));
dynamicProperties.property("EdgeID", boost::get(&Edge::ID, graph));
std::ifstream file("graph.txt");
try
{
boost::read_graphviz(file, graph, dynamicProperties, "NodeID");
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
return 0;
}
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