0

I'm creating a graph class in c++ but having some problems to insert elements into it. The graph is implemented using a vector<vector<Edge> > simulating a linked_list.

Here is my main.cc: http://pastebin.com/xkamm7Jq

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include "edge.h"
#include "graph.h"
#include "vertex.h"
using namespace std;
int main (){
 int n, m;
 cin >> n >> m;
 Graph g (n); 
 for (int i=0; i<m; i++){
 int node, cost;
 cin >> node >> cost; 
 g[i].push_back (Edge (node, cost));
 cout << g[i].size() << endl;
 }
 cout << g.size() << endl;
 for (int i=0; i<g.size(); i++){
 cout << g[i].size() << endl;
 } 
 return 0;
}

Graph.h

#include <iostream>
#include <vector>
#include "vertex.h"
#ifndef _graph_h
#define _graph_h
class Graph {
 private:
 std::vector<Vertex> graph;
 public:
 Graph ();
 Graph (int size);
 Graph (int size, Vertex vertices);
 ~Graph();
 int size ();
 void resize (int size);
 void push_back (Vertex vertex);
 Vertex operator [] (int pos);
};
#endif

Here is graph.cc:

#include "graph.h"
Graph::Graph (){
 //
}
Graph::Graph (int size){
 this->graph.resize (size);
}
Graph::Graph (int size, Vertex vertices){
 this->graph.resize (size);
 for (int i=0; i<size; i++)
 this->graph[i] = vertices;
}
Graph::~Graph (){
 std::cout << "destruiu grafo\n";
}
int Graph::size (){
 return this->graph.size();
}
void Graph::resize (int size){
 this->graph.resize (size);
}
void Graph::push_back (Vertex vertex){
 this->graph.push_back (vertex);
}
Vertex Graph::operator [] (int pos){
 return this->graph[pos];
}

Vertex.h:

#include <vector>
#include "edge.h"
#ifndef _vertex_h
#define _vertex_h
class Vertex {
 private:
 int node;
 std::vector<Edge> edges;
 public:
 Vertex ();
 Vertex (int node);
 Vertex (int node, std::vector<Edge> edges);
 Vertex (const Vertex& other);
 ~Vertex ();
 int getNode ();
 void push_back (Edge edge);
 std::vector<Edge> getEdges ();
 int size();
 Vertex operator = (const Vertex& other);
 Edge operator [] (int pos);
};
#endif

Vertex.cc

#include "vertex.h"
#include <iostream>
Vertex::Vertex (){
}
Vertex::Vertex (int node){
 this->node = node;
}
Vertex::Vertex (int node, std::vector<Edge> edges){
 this->node = node;
 this->edges = edges;
}
Vertex::Vertex (const Vertex& other){
 this->node = other.node;
 this->edges = other.edges;
}
Vertex::~Vertex (){
}
Vertex Vertex::operator = (const Vertex& other){
 this->node = other.node;
 this->edges = other.edges;
 return *this;
}
int Vertex::getNode (){
 return this->node;
}
std::vector<Edge> Vertex::getEdges (){
 return this->edges;
}
void Vertex::push_back (Edge edge){
 this->edges.push_back (edge);
}
int Vertex::size (){
 return this->edges.size();
}
Edge Vertex::operator [] (int pos){
 return this->edges[pos];
}

Edge.h:

#ifndef _edge_h
#define _edge_h
class Edge {
 private:
 int node;
 int cost; 
 public:
 Edge (int node, int cost);
 Edge (const Edge& other);
 ~Edge ();
 Edge operator = (const Edge& other);
 void setNode (int node);
 int getNode ();
 int getCost ();
};
#endif

and Edge.cc:

#include <iostream>
#include "edge.h"
Edge::Edge (int node, int cost){
 this->node = node;
 this->cost = cost;
}
Edge::Edge (const Edge& other){
 this->node = other.node;
 this->cost = other.cost;
}
Edge::~Edge (){
 std::cout << "edge deleted\n";
}
Edge Edge::operator = (const Edge& other){
 this->node = other.node;
 this->cost = other.cost; 
 return *this;
}
int Edge::getNode (){
 return this->node;
}
int Edge::getCost (){
 return this->cost;
}
void Edge::setNode (int node){
 this->node = node;
}

My problem is related to the g.push_back(). Just after the push, the element (in this case, Edge) is deleted from the vector<Edge>.

I know that a vector copies the element during the insertion but apparently, he is holding a reference to the object declared on main ().

Here is an example:

 for (int i=0; i<m; i++){
 int node, cost;
 cin >> node >> cost;
 g[i].push_back (Edge (node, cost));
 cout << g[i].size() << endl;
 }

The cout will always give me 0.

Thanks in advance

asked Oct 14, 2014 at 3:45
9
  • What do you mean by "the element is deleted from the vector"? You means the destructor is called? Commented Oct 14, 2014 at 3:51
  • Just after the insertion, the size of the vector back to be 0. As if I had not inserted anything Commented Oct 14, 2014 at 3:53
  • Yes, the destructor of the Edge () is called. Commented Oct 14, 2014 at 3:54
  • 2
    You'll get more/better answers if you 1. create a minimal example showing the issue. and 2. Put the code for that minimal example in your question, rather than forcing people off-site. Commented Oct 14, 2014 at 3:55
  • 3
    Graph::operator[] returns a vector by value - that is, it returns a temporary. It is in that temporary that you push_back. Of course, the temporary is destroyed soon afterwards. The second call to g[i] produces a fresh, empty temporary vector. Commented Oct 14, 2014 at 4:09

1 Answer 1

1

Can you try this?

Vertex& Graph::operator [] (int pos){
 return this->graph[pos];
}

And add another just for retrieval

const Vertex& Graph::operator [] (int pos)const{
 return this->graph[pos];
}

And just a reminder, not related to the question, but it is good to make use of const for all get methods.

answered Oct 14, 2014 at 4:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.