无法识别 .h 文件中定义的 typedef 类型
Cannot recognise the typedef type defined in .h file
我在让编译器识别 .tem 文件中函数模板的 return 类型时遇到问题(用于将实现与 .h 文件分开)。
In file included from tests/Graph.h:57:0,
from tests/test1.cpp:3:
tests/Graph.tem:28:2: error: ‘reference’ does not name a type
reference Node_Iterator<N, E>::operator*() const {
^~~~~~~~~
tests/Graph.tem:33:2: error: ‘pointer’ does not name a type
pointer Node_Iterator<N, E>::operator->() const {
不确定我做错了什么。我使用 typedef
来定义 return 类型的类型。
/*
* File: Graph.h
*/
#ifndef _Graph_h
#define _Graph_h
#include <vector>
#include <algorithm>
#include <string>
#include <memory>
#include <iostream>
#include <exception>
#include <map>
#include <set>
#include <typeinfo>
namespace gdwg {
template <typename N, typename E> class Graph; // function prototype for Graph class
//-----------------------------------------------------------
// Iterators for Graph with Nodes N and Edges E
//-----------------------------------------------------------
// Iterator class for Node N
template <typename N, typename E> class Node_Iterator {
public:
typedef typename Graph<N, E>::Node Node;
typedef std::ptrdiff_t difference_type;
typedef std::forward_iterator_tag iterator_category;
const typedef N value_type;
const typedef N* pointer;
const typedef N& reference;
// operators for value and reference types
reference operator*() const;
pointer operator->() const;
private:
bool end;
typename std::vector< std::shared_ptr<Node> >::iterator it;
typename std::vector< std::shared_ptr<Node> > mynodes_;
};
#include "Graph.tem" // definition/implementation of Graph, Node_Iterator and Edge_Iterator classes
}
#endif
这是 .tem 文件,它是 Graph.h 文件的实现,我将其包含在 .h 文件的底部。
#include <vector>
#include <algorithm>
#include <string>
#include <memory>
#include <iostream>
#include <exception>
#include <map>
#include <set>
#include <typeinfo>
namespace gdwg {
//-----------------------------------------------------------
// Implementation of Iterator class for Node N
//-----------------------------------------------------------
// operators for value and reference types
template <typename N, typename E>
reference Node_Iterator<N, E>::operator*() const {
return (*it)->val_;
}
template <typename N, typename E>
pointer Node_Iterator<N, E>::operator->() const {
return &(operator*());
}
}
为其添加限定名(带typename
关键字,见Where and why do I have to put the “template” and “typename” keywords?),如:
typename Node_Iterator<N, E>::reference ...
typename Node_Iterator<N, E>::pointer...
在Graph.h
中,#include "Graph.tem"
是在命名空间gdwg
的定义中,而在Graph.tem
中,函数是在另一个命名空间中定义的gdwg
,然后它们将在命名空间 gdwg::gdwg
中定义。您可以将 #include "Graph.tem"
移出 Graph.h
中的命名空间定义,或者删除 Graph.tem
.
中的命名空间定义
我在让编译器识别 .tem 文件中函数模板的 return 类型时遇到问题(用于将实现与 .h 文件分开)。
In file included from tests/Graph.h:57:0,
from tests/test1.cpp:3:
tests/Graph.tem:28:2: error: ‘reference’ does not name a type
reference Node_Iterator<N, E>::operator*() const {
^~~~~~~~~
tests/Graph.tem:33:2: error: ‘pointer’ does not name a type
pointer Node_Iterator<N, E>::operator->() const {
不确定我做错了什么。我使用 typedef
来定义 return 类型的类型。
/*
* File: Graph.h
*/
#ifndef _Graph_h
#define _Graph_h
#include <vector>
#include <algorithm>
#include <string>
#include <memory>
#include <iostream>
#include <exception>
#include <map>
#include <set>
#include <typeinfo>
namespace gdwg {
template <typename N, typename E> class Graph; // function prototype for Graph class
//-----------------------------------------------------------
// Iterators for Graph with Nodes N and Edges E
//-----------------------------------------------------------
// Iterator class for Node N
template <typename N, typename E> class Node_Iterator {
public:
typedef typename Graph<N, E>::Node Node;
typedef std::ptrdiff_t difference_type;
typedef std::forward_iterator_tag iterator_category;
const typedef N value_type;
const typedef N* pointer;
const typedef N& reference;
// operators for value and reference types
reference operator*() const;
pointer operator->() const;
private:
bool end;
typename std::vector< std::shared_ptr<Node> >::iterator it;
typename std::vector< std::shared_ptr<Node> > mynodes_;
};
#include "Graph.tem" // definition/implementation of Graph, Node_Iterator and Edge_Iterator classes
}
#endif
这是 .tem 文件,它是 Graph.h 文件的实现,我将其包含在 .h 文件的底部。
#include <vector>
#include <algorithm>
#include <string>
#include <memory>
#include <iostream>
#include <exception>
#include <map>
#include <set>
#include <typeinfo>
namespace gdwg {
//-----------------------------------------------------------
// Implementation of Iterator class for Node N
//-----------------------------------------------------------
// operators for value and reference types
template <typename N, typename E>
reference Node_Iterator<N, E>::operator*() const {
return (*it)->val_;
}
template <typename N, typename E>
pointer Node_Iterator<N, E>::operator->() const {
return &(operator*());
}
}
为其添加限定名(带
typename
关键字,见Where and why do I have to put the “template” and “typename” keywords?),如:typename Node_Iterator<N, E>::reference ...
typename Node_Iterator<N, E>::pointer...
在
Graph.h
中,#include "Graph.tem"
是在命名空间gdwg
的定义中,而在Graph.tem
中,函数是在另一个命名空间中定义的gdwg
,然后它们将在命名空间gdwg::gdwg
中定义。您可以将#include "Graph.tem"
移出Graph.h
中的命名空间定义,或者删除Graph.tem
. 中的命名空间定义