简体   繁体   English

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

[英]Declaring a vector in a .h file, initialize in a .cpp file

First of all, I know there are a lot of similar posts on here, but I've been looking through them in for a while and I am having trouble deciphering exactly what I need to do. 首先,我知道这里有很多类似的帖子,但是我一直在仔细阅读它们,因此我很难准确地理解我需要做什么。 So on to my problem: I am converting a Graph class I previously wrote in C to C++. 接下来是我的问题:我正在将以前用C编写的Graph类转换为C ++。 I am having trouble converting arrays to vectors due to how I initialize it. 由于初始化方式,我在将数组转换为向量时遇到了麻烦。 If someone could point me in the right direction that'd be awesome. 如果有人能指出我正确的方向,那就太好了。 You can see that I started trying to convert the array char* color to vector color, but it didn't work and the G++ error message was long a cryptic. 您可以看到我开始尝试将数组char * color转换为vector color,但是它不起作用,并且G ++错误消息长期以来一直是一个晦涩的问题。 I left out the #include statements, but I promise those aren't the problem. 我省略了#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 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;
    }       
}

I can't just do vector.push_back() because the rest of the code depends on the random access property of arrays, so they have to be ready to go here. 我不能只做vector.push_back(),因为其余的代码取决于数组的随机访问属性,因此它们必须准备好进入这里。 I'm too new to C++ and am still struggling with the syntax. 我对C ++太陌生,仍然在语法上苦苦挣扎。

EDIT: I guess I should have mentioned that this uses the edge list form of a graph. 编辑:我想我应该提到这使用图形的边列表形式。 The List is a class that I also wrote, which deals with nodes. List是我也写的一个类,它处理节点。 I just have all of these C arrays that I need converted to C++ vectors, and the syntax is killing me. 我只有所有需要转换为C ++向量的C数组,而语法却使我丧命。 For example, the int* distance array should be a vector of ints, and the List** edgeDist and whatnot should be a vector of List*. 例如,int *距离数组应该是ints的向量,而List ** edgeDist和whatnot应该是List *的向量。 Its just initializing it in the Graph.cpp function Graph::prepGraph(int n) that I need help with. 它只是在需要帮助的Graph.cpp函数Graph :: prepGraph(int n)中对其进行了初始化。 The syntax is a little butchered, but I was trying to show what I am trying to do without totally ruining it. 语法有些刺耳,但我试图显示自己想要做的事情而不完全破坏它。 In other words, those static_cast(calloc(whatever)) statements that I keep getting complaints over? 换句话说,那些我不断抱怨的static_cast(calloc(whatever))语句? Help me get rid of those. 帮我摆脱那些。

You have many problems in your code. 您的代码中有很多问题。 I'll try to give you a start: 我会尝试给你一个开始:

//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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM