在C ++中引用向量

时间:2016-07-14 18:36:48

标签: c++ vector graph reference

首先,请知道我已经对我的问题的解决方案进行了广泛的研究,但没有一个解决方案似乎有效。我正在尝试创建一个图表。长话短说,我似乎无法为我的任何节点添加边缘,因为这样做的功能:

void pushEdge(node * dest, int weight) {
    edges.push_back(edge(this,dest,weight));
}

不引用实际的“边缘”向量。当我打印出边缘时,没有打印出来,因为尽管我之前添加了它们,但每个节点显然没有边缘。

如何在类函数中引用类向量?

代码:(如果你愿意的话,运行它,你会看到有0条边。

graph.h - 存储边,节点和图表类。

#pragma once
#include <vector>
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

class node;

class edge {
public:
    edge(){}
    edge(node * origin, node * dest, int weight) {
        this->origin = origin;
        this->dest = dest;
        this->weight = weight;
    }
    node * origin;
    node * dest;
    int weight;

 };

class node {
public:
node(){}
node(int x, int y, int t) {
    this->x = x;
    this->y = y;
    this->t = t;
}

vector<edge> edges;
int x; int t; int y;

void pushEdge(node * dest, int weight) {
    edges.push_back(edge(this,dest,weight));
}

void printEdges() {
    cout << "NODE (" << this->x << "," << this->y << "): " << edges.size() << " edges!" << endl;
    for (int i = 0; i < edges.size(); i++) {
        edge at = edges.at(i);
        cout << "EDGE: Origin(" << at.origin->x << "," << at.origin->y << ") Target(" << at.dest->x << "," << at.dest->y << ") Weight: " << at.weight << endl;
    }
}
};

class graph {
public:
graph(){}
node nodes[900] = {};

void printNodes() {
    cout << "Node count: " << sizeof(this->nodes) << endl;
    for (int i = 0; i < 900; i++) {
        this->nodes[i].printEdges();
    }
}
};

source.cpp - 生成图表并将其打印出来。

#include <iostream>
#include "graph.h"

using namespace std;

graph genGraph();

int main() {
graph myGraph = genGraph();
myGraph.printNodes();
}

graph genGraph() {
graph myGraph;
int i = 0;
for (int x = 1; x <= 30; x++) {
    for (int y = 1; y <= 30; y++) {
        node myNode(x, y, 0);
        myGraph.nodes[i] = myNode;
        i += 1;
    }
}
for (int i = 0; i < 900; i++) {
    node myNode = myGraph.nodes[i];
    int x = myNode.x; int y = myNode.y;
    if (x > 1) {
        node nodeLeft = myGraph.nodes[i-30];
        myNode.pushEdge(&nodeLeft, 2);
        if (y > 1) {
            node nodeTopLeft = myGraph.nodes[i-31];
            myNode.pushEdge(&nodeTopLeft, 2);
        }
        if (y < 30) {
            node nodeBottomLeft = myGraph.nodes[i-29];
            myNode.pushEdge(&nodeBottomLeft, 2);
            cout << myNode.edges.size() << endl;
        }
    }
    if (x < 30) {
        node nodeRight = myGraph.nodes[i+30];
        myNode.pushEdge(&nodeRight, 2);
        if (y > 1) {
            node nodeTopRight = myGraph.nodes[i+29];
            myNode.pushEdge(&nodeTopRight, 2);
        }
        if (y < 30) {
            node nodeBottomRight = myGraph.nodes[i + 31];
            myNode.pushEdge(&nodeBottomRight, 2);
        }
    }
    if (y > 1) {
        node nodeTop = myGraph.nodes[i - 1];
        myNode.pushEdge(&nodeTop, 2);
    }
    if (y < 30) {
        node nodeBottom = myGraph.nodes[i + 1];
        myNode.pushEdge(&nodeBottom, 2);
    }
}
return myGraph;
}

谢谢, Max K

1 个答案:

答案 0 :(得分:1)

似乎问题出在

node myNode = myGraph.nodes[i];

当你在那里创建节点i的副本。您可以创建参考:

node& myNode = myGraph.nodes[i];