Solved

BOOST: GRAPH    Demo Code

Posted on 2007-11-22
8
3,506 Views
Last Modified: 2010-04-21
Hello,

Im new to the Boost Graph lib. I want to run the demp code lines from the tutorial page:
  http://www.boost.org/libs/graph/doc/quick_tour.html

I failed running the template code with a error message I can't help myself.

Here ist the compilers (MS Visual C++)  feed back .-), the code comes in the attachment
----------------------------------------------------------------------------------------------------------------------

------ Build started: Project: boost_demo_v01, Configuration: Debug Win32 ------
Compiling...
boost_demo_v01.cpp
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
        c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(40) : see reference to class template instantiation 'exercise_vertex<Graph>' being compiled
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(20) : error C2143: syntax error : missing ',' before '&'
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
        c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(147) : see reference to class template instantiation 'exercise_vertex<Graph>' being compiled
        with
        [
            Graph=Graph
        ]
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(20) : error C2143: syntax error : missing ',' before '&'
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(147) : error C2440: '<function-style-cast>' : cannot convert from 'Graph' to 'exercise_vertex<Graph>'
        with
        [
            Graph=Graph
        ]
        No constructor could take the source type, or constructor overload resolution was ambiguous
Build log was saved at "file://c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\Debug\BuildLog.htm"
boost_demo_v01 - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




// boost_demo_v01.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.

//
 

  #include "stdafx.h"

  #include <iostream>                  // for std::cout

  #include <utility>                   // for std::pair

  #include <algorithm>                 // for std::for_each

  #include <boost/graph/graph_traits.hpp>

  #include <boost/graph/adjacency_list.hpp>

  #include <boost/graph/dijkstra_shortest_paths.hpp>
 
 

  using namespace boost;
 
 
 
 

template <class Graph> struct exercise_vertex {

    //...

    void operator()(const Vertex& v) const

    {

      typedef graph_traits<Graph> GraphTraits;

      typename property_map<Graph, vertex_index_t>::type 

        index = get(vertex_index, g);
 

      std::cout << "out-edges: ";

      typename GraphTraits::out_edge_iterator out_i, out_end;

      typename GraphTraits::edge_descriptor e;

      for (tie(out_i, out_end) = out_edges(v, g); 

           out_i != out_end; ++out_i) {

        e = *out_i;

        Vertex src = source(e, g), targ = target(e, g);

        std::cout << "(" << index[src] << "," 

                  << index[targ] << ") ";

      }

      std::cout << std::endl;

      //...

    }

    //...

  };
 
 
 
 
 
 

int _tmain(int argc, _TCHAR* argv[])

{
 

/*Constructing a Graph

In this example we will use the BGL adjacency_list class to demonstrate the main ideas in the 

BGL interface. The adjacency_list class provides a generalized version of the classic "adjacency list" 

data structure. The adjacency_list is a template class with six template parameters, though here we only fill 

in the first three parameters and use the defaults for the remaining three. The first two template arguments (

vecS, vecS) determine the data structure used to represent the out-edges for each vertex in the graph and 

the data structure used to represent the graph's vertex set (see section Choosing the Edgelist and VertexList 

for information about the tradeoffs of the different data structures). The third argument, bidirectionalS, 

selects a directed graph that provides access to both out and in-edges. The other options for the third 

argument are directedS which selects a directed graph with only out-edges, and undirectedS which selects 

an undirected graph. 
 

Once we have the graph type selected, we can create the graph in Figure 2 by declaring a graph object 

and filling in edges using the add_edge() function of the MutableGraph interface (which adjacency_list implements).

We use the array of pairs edge_array merely as a convenient way to explicitly create the edges for this example.*/ 
 
 
 
 

    // create a typedef for the Graph type

    typedef adjacency_list<vecS, vecS, bidirectionalS> Graph;
 

    // Make convenient labels for the vertices

    enum { A, B, C, D, E, N };

    const int num_vertices = N;

    const char* name = "ABCDE";
 

    // writing out the edges in the graph

    typedef std::pair<int, int> Edge;

    Edge edge_array[] = 

    { Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C),

      Edge(C,E), Edge(B,D), Edge(D,E),Edge(5,5),Edge(A,5), Edge(C,7), Edge(B,7), Edge(D,8),Edge(5,8),Edge(9,5) ,Edge(9,8),Edge(9,8)};

    const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
 

    // 
 

	std::cout  << "Version 01.02.03 \n\n";
 
 

    // declare a graph object

    Graph g(num_vertices);
 
 

    // add the edges to the graph object

    for (int i = 0; i < num_edges; ++i)

      add_edge(edge_array[i].first, edge_array[i].second, g);
 

//Accessing the Vertex Set

//Now that we have created a graph, we can use the graph interface to access the graph data in different ways. 

//First we can access all of the vertices in the graph using the vertices() function of the VertexListGraph 

//interface. This function returns a std::pair of vertex iterators (the first iterator points to the "beginning" 

//of the vertices and the second iterator points "past the end"). Dereferencing a vertex iterator gives a 

//vertex object. The type of the vertex iterator is given by the graph_traits class. Note that different 

//graph classes can have different associated vertex iterator types, which is why we need the graph_traits class. 

//Given some graph type, the graph_traits class will provide access to the vertex_iterator type. 

//

//The following example prints out the index for each of the vertices in the graph. All vertex and edge properties, 

//including index, are accessed via property map objects. The property_map class is used to obtain the property map

//type for a specific property (specified by vertex_index_t, one of the BGL predefined properties) and function

//call get(vertex_index, g) returns the actual property map object.
 
 

	typedef property_map<Graph, vertex_index_t>::type IndexMap;

    IndexMap index = get(vertex_index, g);
 

    std::cout << "vertices(g) = ";

    typedef graph_traits<Graph>::vertex_iterator vertex_iter;

    std::pair<vertex_iter, vertex_iter> vp;

    for (vp = vertices(g); vp.first != vp.second; ++vp.first)

      std::cout << index[*vp.first] <<  " ";

    std::cout << std::endl;
 

//Accessing the Edge Set

//The set of edges for a graph can be accessed with the edges() function of the EdgeListGraph interface. 

//Similar to the vertices() function, this returns a pair of iterators, but in this case the iterators 

//are edge iterators. Dereferencing an edge iterator gives an edge object. The source() and target() 

//functions return the two vertices that are connected by the edge. Instead of explicitly creating a std::pair 

//for the iterators, this time we will use the tie() helper function. This handy function can be used to assign 

//the parts of a std::pair into two separate variables, in this case ei and ei_end. This is usually more convenient 

//than creating a std::pair and is our method of choice for the BGL.
 

    std::cout << "edges(g) = ";

    graph_traits<Graph>::edge_iterator ei, ei_end;

    for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)

        std::cout << "(" << index[source(*ei, g)] 

                  << "," << index[target(*ei, g)] << ") ";

    std::cout << std::endl;
 

// -----------------
 

//  The Adjacency Structure

//  will encapsulate this in an "exercise vertex" function, and apply it to each vertex in the graph. 
 
 

//    std::for_each(vertices(g).first, vertices(g).second, exercise_vertex<Graph>(g));
 

   std::for_each(vertices(g).first, vertices(g).second,

                     exercise_vertex<Graph>(g));
 
 

	system ("pause");
 
 

    return 0;
 

}

Open in new window

0
Comment
Question by:BdLm
  • 4
  • 4
8 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Can you indicate which lines in the code the errors refer to ?

If the first error refers to this line :

    void operator()(const Vertex& v) const

then check whether you included the header that contains the definition of Vertex.
0
 
LVL 8

Author Comment

by:BdLm
Comment Utility
Hi,

the compiler claims line 20   void operator()(const Vertex& v) const,
Do I need a Header File for my template, how should this look like ?

Best

0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> Do I need a Header File for my template

You need to #include the header that contains the definition of Vertex.
0
 
LVL 8

Author Comment

by:BdLm
Comment Utility
I wrote a header file, but things turned to be even more wrong .->
............................................

------ Build started: Project: boost_demo_v01, Configuration: Debug Win32 ------
Compiling...
boost_demo_v01.cpp
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\template_header.h(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
        c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\template_header.h(8) : see reference to class template instantiation 'exercise_vertex<Graph>' being compiled
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\template_header.h(8) : error C2143: syntax error : missing ',' before '&'
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\template_header.h(8) : error C2143: syntax error : missing ';' before '}'
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(42) : error C2953: 'exercise_vertex' : class template has already been defined
        c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\template_header.h(8) : see declaration of 'exercise_vertex'
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(149) : error C2440: '<function-style-cast>' : cannot convert from 'Graph' to 'exercise_vertex'
        The target type has no constructors
c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\boost_demo_v01.cpp(149) : error C3861: 'exercise_vertex': identifier not found
Build log was saved at "file://c:\layout_extraction_software\codes\c++\boost_v01\boost_demo_v01\Debug\BuildLog.htm"
boost_demo_v01 - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

//

//   Header File for the Template

//  filename :    Template_Header.h

//  date      :     22-11-2007

//
 
 

template <class Graph> struct exercise_vertex {   void operator()(const Vertex& v) const  }; 

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> I wrote a header file, but things turned to be even more wrong .->

That's not what I said ...


Where is Vertex defined ?
0
 
LVL 8

Author Comment

by:BdLm
Comment Utility
Hi,

I did not find the defintion of a vertex, may be somewhere inside the include files,
I found another example which looks like my code trial ....
http://www.boost.org/libs/graph/example/quick_tour.cpp

This one I can compile, the template above I can#t compile ....

Best

http://www.boost.org/libs/graph/example/quick_tour.cpp

Open in new window

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
Comment Utility
>> I did not find the defintion of a vertex,

Well, if you want to use Vertex, you need to have it defined somewhere.


>> may be somewhere inside the include files,

Can you check ?


>> This one I can compile

Not surprisingly, you have this line in that .cpp file :

  typedef typename graph_traits<Graph>::vertex_descriptor Vertex;

which defines Vertex.
0
 
LVL 8

Author Closing Comment

by:BdLm
Comment Utility
bast on the advise I found the bug
0

Featured Post

IT, Stop Being Called Into Every Meeting

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

771 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

7 Experts available now in Live!

Get 1:1 Help Now