我正在尝试学习 C++,并希望实现一些用于在图中查找最小生成树的算法。但是,我在编写界面时遇到了一些麻烦,我不知道我哪里出错了。我收到两个错误:
错误:变量 'Graph::adjIterator it' 具有初始化程序但类型不完整
错误:预期的 ',' 或 ';' 在'='标记之前
图.h
#ifndef GRAPH_H
#define GRAPH_H
#include<vector>
struct Edge {
int v, w;
double weight;
Edge(int v_, int w_, double weight_ = 1) :
v(v_), w(w_), weight(weight_) {
}
};
class Graph {
private:
int Vcnt, Ecnt;
bool directedGraph;
struct Node {
int v;
Node* next;
Node(int v_, Node* next_) :
v(v_), next(next_) {
}
};
std::vector<Node*> adj; //this is a linked list !
public:
Graph(int V, bool diGraph = false) :
adj(V), Vcnt(V), Ecnt(0), directedGraph(diGraph) {
adj.assign(V, NULL);
}
int V() {
return Vcnt;
}
int E() {
return Ecnt;
}
bool directed() const {
return directedGraph;
}
void insert(Edge e) {
int v = e.v;
int w = e.w;
adj[v] = new Node(w, adj[v]);
if (!directedGraph)
adj[w] = new Node(v, adj[w]);
Ecnt++;
}
bool egde(int v, int w) const;
//void remove(Edge e);
class adjIterator;
friend class adjIterator;
};
图.cpp
#include "graph.h"
class Graph::adjIterator {
private:
const Graph & G;
int v;
Node* t;
public:
adjIterator(const Graph& G_, int v_) :
G(G_), v(v_) {
t = 0;
}
int begin() {
t = G.adj[v];
return t ? t->v : -1;
}
int nxt() {
if (t)
t = t->next;
return t ? t->v : -1;
}
bool end() {
return t == 0;
}
};
主文件
#include <iostream>
#include "graph.h"
int main() {
Graph G(2);
Edge e(0, 1);
G.insert(e);
for(Graph::adjIterator it(G,0) = it.begin(); it != it.end(); it.nxt()) {
//stuff
}
return 0;
}
在此先感谢您的帮助。