在.h文件中声明矢量,在.cpp文件中初始化

Declaring a vector in a .h file, initialize in a .cpp file

本文关键字:文件 cpp 初始化 声明      更新时间:2023-10-16

首先,我知道这里有很多类似的帖子,但是我已经看了一段时间了,我很难准确地破译我需要做什么。因此,我的问题是:我正在将以前用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
}