2015-10-10 59 views
0

試圖與BGL啓動,這意味着我開始有很多類型定義的:C++的typedef要求::類型

#include <iostream> //std::cin, std::cout 
#include <tuple> //std::tie 

#include <boost/graph/graph_traits.hpp> 
#include <boost/graph/adjacency_list.hpp> 

using namespace boost; 
typedef adjacency_list<vecS,vecS,undirectedS,no_property,property<edge_weight_t,int> > Graph; 
typedef graph_traits<Graph> Traits; 
typedef Traits::vertex_descriptor Vertex; 
typedef Traits::edge_descriptor Edge; 
typedef property_map<Graph, edge_weight_t>::type EdgeWeightMap; //::type necessary (why?) 

int main(int argc, char* argv[]){ 
    int n = ...; 
    Graph g(n); 
    EdgeWeightMap weight_of; 
    Edge e; 
    bool success; 
    int s,t,w; 
    std::cin >> s >> t >> w; 
    tie(e,success) = add_edge(s,t,g); 
    if(success)weight_of[e] = w; 
} 

,我想知道爲什麼剛剛在的typedef EdgeWeightMap::type是必要的。如果我忽略它,我得到

error: no match for ‘operator[]’ (operand types are

‘EdgeWeightMap {aka boost::property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, int> >, boost::edge_weight_t>}’ 

and

‘Edge {aka boost::detail::edge_desc_impl<boost::undirected_tag, unsigned int>}’ 

) weight_of[e] = w;

(很抱歉的格式,該類型定義<>似乎與塊引用來干擾)

事實上,當我嘗試

EdgeWeightMap weight_of = get(edge_weight,g); 

我得到一個

error: conversion from

‘boost::detail::adj_list_any_edge_pmap::bind_<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, int> >, boost::property<boost::edge_weight_t, int>, boost::edge_weight_t>::type {aka boost::adj_list_edge_property_map<boost::undirected_tag, int, int&, unsigned int, boost::property<boost::edge_weight_t, int>, boost::edge_weight_t>}’ 

to non-scalar type

‘EdgeWeightMap {aka boost::property_map<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::property<boost::edge_weight_t, int> >, boost::edge_weight_t>}’ 

requested EdgeWeightMap weight_of = get(edge_weight,g);

現在,我可以看到這些不同類型的,我不明白的是爲什麼他們不同。因爲我喜歡避免意外,有人請告訴我什麼時候::type是必需的,什麼時候不能使用?

回答

1

從閱讀property_map文檔,「property_map」模板旨在提供類型,而不是類型。 property_map結構恰好包含兩種類型。 typeconst_type,分別爲可變和不可變類型。