我编写了这些代表图形的类:
class Node{
public:
typedef std::vector<Node>::iterator iterator;
iterator begin() {return neigh.begin();}
iterator end() {return neigh.end();}
public:
Node(std::string n) : name(n) {}
std::string getName() {return name; }
void addNeigh(Node n){
neigh.push_back(n);
}
private:
std::string name;
std::vector<Node> neigh;
};
class Map{
public:
typedef std::vector<Node>::iterator iterator;
iterator begin() {return nodes.begin();}
iterator end() {return nodes.end();}
public:
Map(std::string n) : map_name(n) {}
void addNode(Node n){
nodes.push_back(n);
}
std::string getName() { return map_name; }
int getNumNodes() {return nodes.size();}
private:
std::string map_name;
std::vector<Node> nodes;
};
然后我想编写一个printGraph
使用特征打印一些图形信息的函数:
template<typename T>
struct GraphTraits{
};
template<>
struct GraphTraits<Map>{
typedef Map::iterator node_iterator;
static node_iterator node_begin(Map map){ return map.begin();}
static node_iterator node_end(Map map) { return map.end();}
static std::string getName(Map m) {return m.getName();}
};
template<typename T>
void printGraph(T t){
std::cout << std::endl
<< "--------------------------------------" << std::endl
<< "| printGraph |" << std::endl
<< "--------------------------------------" << std::endl;
std::cout << "Graph name: " << GraphTraits<T>::getName(t) << std::endl;
for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
it != e; ++it) {
std::cout<< (*it).getName() << std::endl; }
}
如果我尝试编译此代码,我会收到以下错误:
grafo.cpp:75:37: error: expected ';' in 'for' statement specifier
for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
^
grafo.cpp:75:37: error: use of undeclared identifier 'it'
grafo.cpp:75:73: error: use of undeclared identifier 'e'
for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
^
grafo.cpp:76:9: error: use of undeclared identifier 'it'
it != e; ++it) {
^
grafo.cpp:76:15: error: use of undeclared identifier 'e'
it != e; ++it) {
^
grafo.cpp:76:16: error: expected ')'
it != e; ++it) {
^
grafo.cpp:75:6: note: to match this '('
for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
^
grafo.cpp:76:20: error: use of undeclared identifier 'it'
it != e; ++it) {
^
grafo.cpp:75:23: error: unexpected type name 'node_iterator': expected expression
for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
^
grafo.cpp:104:3: note: in instantiation of function template specialization 'printGraph<Map>' requested here
printGraph(mappa);
^
我可以解决并成功编译替换:
for(GraphTraits<T>::node_iterator....
和:
for(GraphTraits<Map>::node_iterator....
但这使得printGraph
它失去了普遍性。
有人可以给我一些提示来解决问题吗?
一般来说,是否可以使用 Traits 来强制定义数据类型?我是说:
template<typename T>
struct ATraits{
-> type "iterator" must be defined
};
这样每个专业都必须定义:
typedef ..something.. iterator;