图形实现:变量具有初始值设定项,但类型不完整
graph implementation: variable has initializer but incomplete type
我正在尝试学习C++,并希望实现一些算法来查找图中的最小生成树。但是,我在编写界面时遇到了一些问题,我不知道哪里出了问题。我得到两个错误:
错误:变量"图形::adjIterator iterator"具有初始值设定项,但类型不完整
错误:在"="标记之前应使用","或";"
图形.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;
}
};
主.cpp
#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;
}
提前感谢您的帮助。
您已经在.cpp文件中定义了类"adjIterator",然后尝试从另一个.cpp文件中使用它,该文件仅在.h中看到前向声明
此外,虽然这不是您的直接问题,但您的 .h 文件中有很多可能属于.cpp的东西。
通常,您将所有声明放在 .h 中,将所有实现放在一个.cpp中。
所以一个.h可能有:
class myClass {
public:
myClass();
void someMethod(int argument);
}
然后.cpp将具有:
myClass::myClass()
{
//initialise stuff
}
void myClass::someMethod(int argument)
{
//do something clever
}
相关文章:
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 指针数据类型变量如何包含对象?
- 将 static_cast<int>(-15) 分配给静态常量字符类型变量
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- 如何为结构类型变量赋值
- 如何将字符类型变量附加到字符串中?
- 共享或私有 openmp 代码中的结构化类型变量
- 有没有办法在 c++ 中制作类型变量?
- 从其他类声明类类型变量会给出 C2143 错误(在"*"之前缺少";")
- 如何检查类中定义的枚举类型变量是否被分配给?
- 将子类实例保存在父类型变量中并通过父变量使用 Child 函数?
- 是否可以在没有结构的情况下创建新的类型变量?
- 为什么类型变量;不调用默认 CTR
- 如何使用CIN将输入整数值获取到char类型变量中
- 如何为 char** 类型变量提供适当的值,以便将其分配给字符串变量
- 在C++标准中哪里说必须初始化 const 内置类型变量的定义
- 为什么可以将其他类型变量用作C 中常量参考参数参数的参数
- Eclipse CDT 打开声明的自动类型变量
- 如何计算长长类型变量中的位数
- 将自动类型变量初始化为零.这种类型安全吗?