在.h文件中声明矢量,在.cpp文件中初始化
Declaring a vector in a .h file, initialize in a .cpp file
首先,我知道这里有很多类似的帖子,但是我已经看了一段时间了,我很难准确地破译我需要做什么。因此,我的问题是:我正在将以前用C编写的Graph类转换为c++。我有麻烦转换数组到向量由于我如何初始化它。如果有人能给我指个方向就太好了。您可以看到,我开始尝试将数组char* color转换为矢量颜色,但它不起作用,并且g++错误消息很长一段时间是模糊的。我省略了#include语句,但我保证这些都不是问题。
Graph.h
using namespace std;
const int INF = INT_MAX;
const int NIL = 0;
class Graph
{
public:
Graph(int n);
Graph(ifstream& in);
~Graph(void);
int getOrder();
int getSize();
int getSource();
int getParent(int u);
int getDist(int u);
void getAdjacencyList(List* L, int u);
void makeNull();
void addEdge(int u, int v, int weight, int color);
void addArc(int u, int v, int weight, int color);
void BFS(int s, int c);
void Prim(int s, int color);
void printGraph();
void printSpanningTree();
private:
bool colorApprover(int, bool ,bool , bool);
void prepGraph(int n);
int order;
int size;
int source;
vector<char> color (16);
int* distance;
int* parent;
List** edgeDist;
List** edgeColor;
List** adj;
};
Graph.cpp
//helper function to streamline the constructors
void Graph::prepGraph(int n){
order = n;
size = 0;
source = NIL;
//color (n + 1);//static_cast<char*>(calloc(n + 1, sizeof(char)));
distance = static_cast<int*>(calloc(n + 1, sizeof(int)));
parent = static_cast<int*>(calloc(n + 1, sizeof(int)));
adj = static_cast<List**>(calloc(n + 1, sizeof(List*)));
edgeDist = static_cast<List**>(calloc(n + 1, sizeof(List*)));
edgeColor = static_cast<List**>(calloc(n + 1, sizeof(List*)));
//discover = static_cast<int*>(calloc(n + 1, sizeof(int)));
//finish = static_cast<int*>(calloc(n + 1, sizeof(int)));
int i;
for(i = 0; i <= n; i++){
color[i] = 'w';
distance[i] = INF;
parent[i] = NIL;
adj[i] = new List();
edgeDist[i] = new List();
edgeColor[i] = new List();
}
}
Graph::Graph(int n){
prepGraph(n);
}
Graph::Graph(ifstream& in){
int n;
in >> n;
prepGraph(n);
while(!in.eof()){
int i, j, weight, color;
in >> i;
in >> j;
in >> weight;
in >> color;
//increment values by 1 so they fit
//into existing graph structure
i += 1;
j += 1;
addEdge(i, j, weight, color);
//cout << "added arc " << i << " " << j << endl;
}
}
我不能只做vector.push_back(),因为剩下的代码依赖于数组的随机访问属性,所以它们必须准备好放到这里。我对c++太陌生了,语法还在纠结。
编辑:我想我应该提到过,这使用了图的边表形式。List是我编写的一个类,它处理节点。我只需要把所有这些C数组转换成c++向量,语法简直要杀了我。例如,int* distance数组应该是int型的向量,而List** edgeDist和whatnot应该是List*的向量。它只是在我需要帮助的Graph.cpp函数Graph::prepGraph(int n)中初始化它。语法有点混乱,但我试图在不完全破坏它的情况下展示我要做的事情。换句话说,那些我一直抱怨的static_cast(calloc(whatever))语句?帮我把这些删掉
你的代码有很多问题。我尽量给你一个开始:
//helper function to streamline the constructors
typedef List<int> NodeList;
struct Node {
char color;
int distance;
int parent;
NodeList adj;
NodeList edgeDist;
NodeList edgeColor;
Node() {
color = 'w';
distance = INF;
parent = 0;
}
}
class Graph
{
...
private:
vector<Node> nodes;
...
}
void Graph::prepGraph(int n){
order = n;
size = 0;
source = NIL;
// nodes will be initialized implicitly
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 为什么我的主文件.cpp不打印头文件中的任何内容
- 生成文件错误 - 找不到文件 - *.cpp
- 如何在文件.cpp gtkmm中声明小部件
- 没有实现文件(.cpp)的派生类
- 如何使用"CMakeLists.txt"中的add_library将整个文件(.cpp,.h等)包含在目录中
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 关于 Linux .so 文件无法链接到主文件.cpp文件
- 文件.cpp从Windows到Linux
- Ubuntu C++ 编译器错误: g++: 错误: 文件.cpp: 没有这样的文件或目录
- 如何将文件.cpp编译为本机编译
- 使用模板类时,似乎无法包含除 main 以外的任何 cpp 文件.cpp
- 无法编译C++文件.cpp。C++98模式
- 为什么C++头文件不需要包含实现文件 (.cpp)
- 如何访问位于独立文件(.cpp)中的非成员函数
- 如何在Visual Studio Code中通过键盘快捷键切换头文件/cpp文件
- 如何将数据从stringstream写入文件(CPP)
- 为什么filestream不把空白写入文件cpp ?