【发布时间】:2015-01-06 04:00:32
【问题描述】:
我正在制作一个程序,其中一个两个类非常密切相关,所以我想在另一个里面声明一个,这样用户就必须使用 Class::member 语法来创建和派生这个类类型。我已经将我的程序抽象为 4 个类并重新创建了错误。课程是:
GraphInterface, Graph, GraphIteratorInterface, and GraphIterator.
Graph 从它的接口和迭代器公开派生的地方。现在因为迭代器只能与这些图一起使用,我希望用户必须通过去创建一个
Graph<int>::GraphIterator<int> it;
我试图通过在 GraphInterface 类中声明 GraphIteratorInterface 类,然后将其定义为单独的 .h 文件来实现这一点。然后我在 Graph 类(派生自 GraphInterface)中声明 GraphIterator,然后像往常一样在它自己的 cpp 文件中定义它。
代码在 ideone 上:http://ideone.com/3fByCl 当我尝试运行所有内容时,我得到了错误
"error: too few template-parameter-lists class GraphInterface<V>::GraphIteratorInterface {"
在其他错误中。我不明白怎么会缺少模板参数,我肯定给了它一个(V)。
编辑 - 代码在这里
#include <iostream>
#include <vector>
using namespace std;
template<class V>
class GraphInterface {
public:
// Abstract GraphIterator class, defined elsewhere
template<class VE> class GraphIteratorInterface;
virtual ~GraphInterface();
// BFS that uses the graph iterator class to perform work on the discovered
// vertices and edges
virtual void BFS(V src_vertex, GraphIteratorInterface<V> *) = 0;
};
template<class V>
class Graph : public GraphInterface<V> {
public :
template<class VE> class GraphIterator; // implements the graph-iter interface
~Graph();
void BFS(V src_vertex, GraphIterator<V> *);
};
template<class V>
class GraphInterface<V>::GraphIteratorInterface {
virtual void examine_edge(/*Edge Object*/) = 0;
virtual void discover_vertex(const V &) = 0;
// ...
};
template<class V>
class Graph<V>::GraphIterator : public GraphInterface<V>::GraphIteratorInterface {
std::vector<V> vertices;
//.. other members not in the interface
void examine_edge(/*Edge Object*/);
void discover_vertex(const V &)
};
int main() {
Graph<int> g;
return 0;
}
【问题讨论】:
-
代码应该嵌入问题而不是链接。
-
我马上改,谢谢指点。我认为你们可以编译它并自己查看错误这一事实会有所帮助。