DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: graph problem

1. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
Make the matrix a private data field of your graph. The createMatrix() function should be a function of the graph, executed after you've added all of the nodes to the graph.

As to your other stuff ... it is swirling around without a clear path for me. I'll have to think about it when my eyes are less bleary.

2. Registered User
Join Date
Nov 2008
Posts
37
Now it is possible to compile the code, but it is still does not work. Here is an output of the program:
Code:
```	--- Node 49 ---
Coordinate:		19.98 63.046 33.535
Adjacent Nodes:	40, 41, 43, 44, 45, 48, 49

--- Node 49 ---
Coordinate:		19.98 63.046 33.535
Adjacent Nodes:	39, 41, 43, 44, 45, 48, 49

--- Node 49 ---
Coordinate:		19.98 63.046 33.535
Adjacent Nodes:	39, 40, 43, 44, 45, 48, 49

--- Node 49 ---
Coordinate:		19.98 63.046 33.535
Adjacent Nodes:	39, 40, 41, 44, 45, 48, 49

--- Node 49 ---
Coordinate:		19.98 63.046 33.535
Adjacent Nodes:	39, 40, 41, 43, 45, 48, 49

--- Node 49 ---
Coordinate:		19.98 63.046 33.535
Adjacent Nodes:	39, 40, 41, 43, 44, 48, 49

--- Node 49 ---
Coordinate:		19.98 63.046 33.535
Adjacent Nodes:	39, 40, 41, 43, 44, 45, 49

--- Node 48 ---
Coordinate:		22.351 66.269 28.727
Adjacent Nodes:	39, 40, 41, 43, 44, 45, 48```
The program only save the last coordinates although the file looks like:
Code:
```39      18.378  59.466  13.185
40      14.270  54.556  18.227
41      30.119  51.333  33.732
43      26.326  51.100  21.154
44      23.764  55.050  18.338
45      17.697  75.884  26.178
48      22.351  66.269  28.727
49      19.980  63.046  33.535```
main.cpp
Code:
```#include "graph.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct s_node {
int label;
double x, y, z;
};

istream &operator>>(istream &in, s_node &n) {
return in >> n.label >> n.x >> n.y >> n.z;
}

int main() {
//make an empty graph, and an empty temporary graph node
Graph graph = Graph();
GraphNode tempNode = GraphNode();
vector<s_node> v_nodes;
s_node s_nodeTmp;

ifstream in("file.txt");
while(in >> s_nodeTmp) {
v_nodes.push_back(s_nodeTmp);
}
//construct the test graph
for (size_t i = 0; i < v_nodes.size(); i++) {
for (size_t j = 0; j < v_nodes.size(); j++) {
if (v_nodes[i].label != v_nodes[j].label){
v_nodes[j].label,
v_nodes[j].x,
v_nodes[j].y,
v_nodes[j].z);
}
}
graph.addNode(tempNode); 	  //add the node to the graph
tempNode.reset();			  //clear our temporary field
}

//print the graph
graph.print();

return 0;
}```
graph.h
Code:
```#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <string>

using namespace std;

//max number of nodes
#define MAX_GRAPH_SIZE 256

class GraphNode
{
private:
string color;  //the color assigned to this node
int adjacencyList[MAX_GRAPH_SIZE]; //list of indices of nodes connected to this node
int label;
double x,y,z; //x,y,z as in a point in 3-d space.

public:
GraphNode(); //constructor
GraphNode(string col, int* list, int numAdj); //constructor

//get and set functions

void setLabel(int label);
void setX(double x);
void setY(double y);
void setZ(double z);

int getLabel();
double getX();
double getY();
double getZ();

//utility functions
void addAdjacentNode(int label, double x, double y,
double z);
void print();
void reset();
void distance();
};

class Graph
{
private:
GraphNode nodes[MAX_GRAPH_SIZE];  //an array of nodes to represent the graph
int numNodes;
double distance_matrix [MAX_GRAPH_SIZE][MAX_GRAPH_SIZE];

public:
Graph(); //constructor
Graph(GraphNode* nodeArray, int num); //constructor

//get functions
int getNumNodes();
GraphNode* getNodes();

//utility functions
void print();
GraphNode getNodeAt(int index);
void setNodeAt(int index, GraphNode node);
};

#endif /* GRAPH_H_ */```
graph.cpp
Code:
```#include "graph.h"

using namespace std;

GraphNode::GraphNode()
{
//default constructor, initializes fields
color = "";
for (int count = 0; count < MAX_GRAPH_SIZE; count++)
{
}
}

GraphNode::GraphNode(string col, int* list, int numAdj)
{
//alternate constructor
color = col;
for (int count = 0; (count < numAdj) && (count < MAX_GRAPH_SIZE); count++)
{
}

}

{
//returns the complete adjacency list
}

{
//returns the number of adjacent nodes
}

void GraphNode::setLabel(int label){this->label = label;}
void GraphNode::setX(double x){this->x = x;}
void GraphNode::setY(double y){this->y = y;}
void GraphNode::setZ(double z){this->z = z;}

int GraphNode::getLabel(){return label;}
double GraphNode::getX(){return x;}
double GraphNode::getY(){return y;}
double GraphNode::getZ(){return z;}

void GraphNode::addAdjacentNode(int label, double x, double y,
double z)
{
setLabel(label); setX(x); setY(y); setZ(z);
}
/*
* Description:  prints the node's color and its current adjacency list to stdout.
*				 this will not print prettily for a node with many adjacent nodes
*
* Paramaters:  [none]
*
* Returns:  [nothing]
*/
void GraphNode::print()
{
cout << "\t--- Node " << label << " ---"<<endl;
cout << "Coordinate:\t\t"
<< getX() << " "
<< getY() << " "
<< getZ() << " "
<< endl;

cout << "Adjacent Nodes:\t";
for (int count = 0; count < numAdjacentNodes; count++)
{
if (count != numAdjacentNodes - 1)
{
cout << ", ";
}
}
cout << endl;
}

/*
* Description:  resets all fields in the node to their default values
*
* Paramaters:  [none]
*
* Returns:  [nothing]
*/
void GraphNode::reset()
{
color = "";
for (int count = 0; count < MAX_GRAPH_SIZE; count++)
{
}
}

void GraphNode::distance(){
double x_p, y_p, z_p;

/*	for(int i= 1; i <= nodeList.size(); ++i)
{
for(int j = 1; j <= nodeList.size(); ++j)
{
if (i == j)
{
distance_matrix[i][j] = 0;
}
else
{
x_p = nodeList[i].getX() - nodeList[j].getX();
y_p = nodeList[i].getY() - nodeList[j].getY();
z_p = nodeList[i].getZ() - nodeList[j].getZ();
this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p );
}
}
}*/
}

Graph::Graph()
{
//default constructor
for (int count = 0; count < MAX_GRAPH_SIZE; count++)
{
nodes[count] = GraphNode();
}
numNodes = 0;
}

Graph::Graph(GraphNode* nodeArray, int num)
{
//alternate constructor
numNodes = 0;
for(int count = 0; (count < num) && (count < MAX_GRAPH_SIZE); count++)
{
nodes[count] = nodeArray[count];
numNodes++;
}

}

int Graph::getNumNodes()
{
//returns the number of nodes in the graph
return numNodes;
}

GraphNode* Graph::getNodes()
{
//returns the list of nodes in the graph
return nodes;
}

/*
* Description:  Adds a new node to the graph
*
* Paramaters:  node (GraphNode) - the node to add
*
* Returns:  [nothing]
*/
{
if (numNodes < MAX_GRAPH_SIZE)
{
nodes[numNodes] = node;
numNodes++;
}
else
{
//this should be a non-void function that returns an error code
//if the graph is full
}
}

/*
* Description:  retrieves the node at a given position (index) in the graph
*
* Paramaters:  index (int) - the index of the node to lookup
*
* Returns:  (GraphNode) - the node at the given index
*/
GraphNode Graph::getNodeAt(int index)
{
if ((index < numNodes) && (index >= 0))
{
return nodes[index];
}
else
{
//invalid index specified
return GraphNode();  //return an 'empty' node
}
}

/*
* Description:  prints the contents of the graph
*
* Paramaters:  [none]
*
* Returns:  [nothing]
*/
void Graph::print()
{
for (int count = 0; count < numNodes; count++)
{
//cout << "\t--- Node " << count << " ---"<<endl;
nodes[count].print();
cout << endl << endl;
}
}

/*
* Description:  assigns the specified node to the specified location in the graph
*
* Paramaters:  index (int) - the location to write to
*				node (GraphNode) - the node to store
*
* Returns:  [nothing]
*/
void Graph::setNodeAt(int index, GraphNode node)
{
//assignments are only allowed into existing locations
if ((index < numNodes) && (index >= 0))
{
nodes[index] = node;
}
}```
Unfortunately, I have still problems with OOP. How it is possible to fix the above problems?

3. Registered User
Join Date
Nov 2008
Posts
37
I tried to solve the problems in above code. Therefore I modefied the code further and now I get following errors:
Code:
```../graph.cpp: In member function 'void GraphNode::distance()':
../graph.cpp:60: warning: unused variable 'x_p'
../graph.cpp:60: warning: unused variable 'y_p'
../graph.cpp:60: warning: unused variable 'z_p'
../graph.cpp: In member function 'GraphNode* Graph::getNodes()':
../graph.cpp:90: error: cannot convert 'std::vector<GraphNode, std::allocator<GraphNode> >' to 'GraphNode*' in return
../graph.cpp: In member function 'GraphNode Graph::getNodeAt(int)':
../graph.cpp:110: warning: control reaches end of non-void function
make: *** [graph.o] Error 1```
main.cpp
Code:
```
#include "graph.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct s_node {
int label;
double x, y, z;
};

istream &operator>>(istream &in, s_node &n) {
return in >> n.label >> n.x >> n.y >> n.z;
}

int main() {
//make an empty graph, and an empty temporary graph node
Graph graph = new Graph();
s_node s_nodeTmp;

ifstream in("template.txt");
//construct the nodes
while(in >> s_nodeTmp) {
//v_nodes.push_back(s_nodeTmp);
GraphNode tempNode = new GraphNode(s_nodeTmp.label,
s_nodeTmp.x,
s_nodeTmp.y,
s_nodeTmp.z);

}
for (int i = 0; i < graph.getNumNodes(); i++) {
for (int j = 0; j < graph.getNumNodes(); j++) {
if (graph.getNodeAt(i).label != graph.getNodeAt(j).label){
}
}
}

//print the graph
graph.print();

return 0;
}```
graph.h
Code:
```#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//max number of nodes

class GraphNode
{
private:
vector<int> adjacencyList; //list of indices of nodes connected to this node
int label;
double x,y,z; //x,y,z as in a point in 3-d space.

public:
GraphNode();
GraphNode(int label, double x, double y, double z);

//get and set functions

void setLabel(int label);
void setX(double x);
void setY(double y);
void setZ(double z);

int getLabel();
double getX();
double getY();
double getZ();

//utility functions
void print();
void distance();
};

class Graph
{
private:
vector<GraphNode> nodes;  //an array of nodes to represent the graph
int numNodes;
vector<vector<double> > distance_matrix;
vector<vector<int> > connects;

public:
Graph();
//get functions
int getNumNodes();
GraphNode* getNodes();

//utility functions
void print();
GraphNode getNodeAt(int index);
void setNodeAt(int index, GraphNode node);

};

#endif /* GRAPH_H_ */```
graph.cpp
Code:
```
#include "graph.h"

using namespace std;

GraphNode::GraphNode(int label, double x, double y, double z){
setLabel(label); setX(x); setY(y); setZ(z);
}

{
//returns the complete adjacency list
}

{
//returns the number of adjacent nodes
}

void GraphNode::setLabel(int label){this->label = label;}
void GraphNode::setX(double x){this->x = x;}
void GraphNode::setY(double y){this->y = y;}
void GraphNode::setZ(double z){this->z = z;}

int GraphNode::getLabel(){return label;}
double GraphNode::getX(){return x;}
double GraphNode::getY(){return y;}
double GraphNode::getZ(){return z;}

}

void GraphNode::print()
{
cout << "\t--- Node " << label << " ---"<<endl;
cout << "Coordinate:\t\t"
<< getX() << " "
<< getY() << " "
<< getZ() << " "
<< endl;

cout << "Adjacent Nodes:\t";
for (int count = 0; count < numAdjacentNodes; count++)
{
if (count != numAdjacentNodes - 1)
{
cout << ", ";
}
}
cout << endl;
}

void GraphNode::distance(){
double x_p, y_p, z_p;

/*	for(int i= 1; i <= nodeList.size(); ++i)
{
for(int j = 1; j <= nodeList.size(); ++j)
{
if (i == j)
{
distance_matrix[i][j] = 0;
}
else
{
x_p = nodeList[i].getX() - nodeList[j].getX();
y_p = nodeList[i].getY() - nodeList[j].getY();
z_p = nodeList[i].getZ() - nodeList[j].getZ();
this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p );
}
}
}*/
}

int Graph::getNumNodes()
{
//returns the number of nodes in the graph
return numNodes;
}

GraphNode* Graph::getNodes()
{
//returns the list of nodes in the graph
return nodes;
}

{
nodes.push_back(node);
numNodes++;
}

GraphNode Graph::getNodeAt(int index)
{
if ((index < numNodes) && (index >= 0))
{
return nodes[index];
}
else
{
//invalid index specified
//		return GraphNode();  //return an 'empty' node
}
}

void Graph::print()
{
for (int count = 0; count < numNodes; count++)
{
//cout << "\t--- Node " << count << " ---"<<endl;
nodes[count].print();
cout << endl << endl;
}
}

void Graph::setNodeAt(int index, GraphNode node)
{
//assignments are only allowed into existing locations
if ((index < numNodes) && (index >= 0))
{
nodes[index] = node;
}
}```
How can I solve the errors?

4. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
You've commented out the body of your distance() function. That's why your x_p, y_p and z_p aren't used.

The other message refers to the datatype of the return data of your getNodes() function. Your nodes vector holds GraphNode objects; with this declaration you are claiming that the method is supposed return a pointer ... yet you haven't created a pointer object anywhere. Do you want to return a GraphNode object or, perhaps, a reference to a GraphNode object? [I don't understand this function - how can one node object represent a vector of node objects?]

5. Registered User
Join Date
Nov 2008
Posts
37
I think I do not need getNodes(). I did not remove getNodes(), because I get following problems without getNodes():

Code:
```make all
Building file: ../graph.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"graph.d" -MT"graph.d" -o"graph.o" "../graph.cpp"
../graph.cpp: In member function 'void GraphNode::distance()':
../graph.cpp:60: warning: unused variable 'x_p'
../graph.cpp:60: warning: unused variable 'y_p'
../graph.cpp:60: warning: unused variable 'z_p'
../graph.cpp: In member function 'GraphNode Graph::getNodeAt(int)':
../graph.cpp:104: warning: control reaches end of non-void function
Finished building: ../graph.cpp

Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp: In function 'int main()':
../main.cpp:22: error: conversion from 'Graph*' to non-scalar type 'Graph' requested
../main.cpp:32: error: conversion from 'GraphNode*' to non-scalar type 'GraphNode' requested
make: *** [main.o] Error 1```
The following code is wouthout getNodes()

main.cpp
Code:
```#include "graph.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct s_node {
int label;
double x, y, z;
};

istream &operator>>(istream &in, s_node &n) {
return in >> n.label >> n.x >> n.y >> n.z;
}

int main() {
//make an empty graph, and an empty temporary graph node
Graph graph = new Graph();
s_node s_nodeTmp;

ifstream in("template.txt");
//construct the nodes
while(in >> s_nodeTmp) {
//v_nodes.push_back(s_nodeTmp);
GraphNode tempNode = new GraphNode(s_nodeTmp.label,
s_nodeTmp.x,
s_nodeTmp.y,
s_nodeTmp.z);

}
for (int i = 0; i < graph.getNumNodes(); i++) {
for (int j = 0; j < graph.getNumNodes(); j++) {
if (graph.getNodeAt(i).getLabel() != graph.getNodeAt(j).getLabel()){
}
}
}

//print the graph
graph.print();

return 0;
}```
graph.h
Code:
```#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//max number of nodes

class GraphNode
{
private:
vector<int> adjacencyList; //list of indices of nodes connected to this node
int label;
double x,y,z; //x,y,z as in a point in 3-d space.

public:
GraphNode();
GraphNode(int label, double x, double y, double z);

//get and set functions

void setLabel(int label);
void setX(double x);
void setY(double y);
void setZ(double z);

int getLabel();
double getX();
double getY();
double getZ();

//utility functions
void print();
void distance();
};

class Graph
{
private:
vector<GraphNode> nodes;  //an array of nodes to represent the graph
int numNodes;
vector<vector<double> > distance_matrix;
vector<vector<int> > connects;

public:
Graph();
//get functions
int getNumNodes();

//utility functions
void print();
GraphNode getNodeAt(int index);
void setNodeAt(int index, GraphNode node);

};
#endif /* GRAPH_H_ */```
Code:
```#include "graph.h"

using namespace std;

GraphNode::GraphNode(int label, double x, double y, double z){
setLabel(label); setX(x); setY(y); setZ(z);
}

{
//returns the complete adjacency list
}

{
//returns the number of adjacent nodes
}

void GraphNode::setLabel(int label){this->label = label;}
void GraphNode::setX(double x){this->x = x;}
void GraphNode::setY(double y){this->y = y;}
void GraphNode::setZ(double z){this->z = z;}

int GraphNode::getLabel(){return label;}
double GraphNode::getX(){return x;}
double GraphNode::getY(){return y;}
double GraphNode::getZ(){return z;}

}

void GraphNode::print()
{
cout << "\t--- Node " << label << " ---"<<endl;
cout << "Coordinate:\t\t"
<< getX() << " "
<< getY() << " "
<< getZ() << " "
<< endl;

cout << "Adjacent Nodes:\t";
for (int count = 0; count < numAdjacentNodes; count++)
{
if (count != numAdjacentNodes - 1)
{
cout << ", ";
}
}
cout << endl;
}

void GraphNode::distance(){
double x_p, y_p, z_p;

/*	for(int i= 1; i <= nodeList.size(); ++i)
{
for(int j = 1; j <= nodeList.size(); ++j)
{
if (i == j)
{
distance_matrix[i][j] = 0;
}
else
{
x_p = nodeList[i].getX() - nodeList[j].getX();
y_p = nodeList[i].getY() - nodeList[j].getY();
z_p = nodeList[i].getZ() - nodeList[j].getZ();
this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p );
}
}
}*/
}

int Graph::getNumNodes()
{
//returns the number of nodes in the graph
return numNodes;
}

{
nodes.push_back(node);
numNodes++;
}

GraphNode Graph::getNodeAt(int index)
{
if ((index < numNodes) && (index >= 0))
{
return nodes[index];
}
else
{
//invalid index specified
//		return GraphNode();  //return an 'empty' node
}
}

void Graph::print()
{
for (int count = 0; count < numNodes; count++)
{
//cout << "\t--- Node " << count << " ---"<<endl;
nodes[count].print();
cout << endl << endl;
}
}

void Graph::setNodeAt(int index, GraphNode node)
{
//assignments are only allowed into existing locations
if ((index < numNodes) && (index >= 0))
{
nodes[index] = node;
}
}```
The reason why I have commented out the body of distance() function is that I still do not understand how I can calculate the distances.

How can I fix the above errors?
Last edited by flaxyz; 05-15-2009 at 07:38 AM.

6. Senior Member
Join Date
Dec 2003
Posts
3,366
post main lines 22 and 32. I tried to do that from your posted code but I think its not the same as what you compiled --- just open main.cpp in an editor that has line numbers, go to those lines, and paste them here.

Most likely, you assign a pointer a value, instead of dereferencing the pointer, on those lines or some similar hard to see pointer mistake (treating a pointer as if it were the data type or something).

edit: I think I found one of them:

GraphNode tempNode = new GraphNode(s_nodeTmp.label,

you just assigned a pointer (the result of NEW is a pointer) into a variable that is NOT a pointer. You missed the *, it should be:

GraphNode *tempNode = new GraphNode(s_nodeTmp.label,

7. Registered User
Join Date
Nov 2008
Posts
37
I changed it to GraphNode *tempNode = new GraphNode(s_nodeTmp.label,
but I get still errors:
Code:
```Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp: In function 'int main()':
../main.cpp:22: error: conversion from 'Graph*' to non-scalar type 'Graph' requested
../main.cpp:33: error: no matching function for call to 'Graph::addNode(GraphNode*&)'
../graph.h:59: note: candidates are: void Graph::addNode(GraphNode)
make: *** [main.o] Error 1```
Line 22 is Graph graph = new Graph(); and line 33 is graph.addNode(tempNode);

What do I wrong?

8. Senior Member
Join Date
Dec 2003
Posts
3,366
Originally Posted by flaxyz
I changed it to GraphNode *tempNode = new GraphNode(s_nodeTmp.label,
but I get still errors:
Code:
```Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
../main.cpp: In function 'int main()':
../main.cpp:22: error: conversion from 'Graph*' to non-scalar type 'Graph' requested
../main.cpp:33: error: no matching function for call to 'Graph::addNode(GraphNode*&)'
../graph.h:59: note: candidates are: void Graph::addNode(GraphNode)
make: *** [main.o] Error 1```
Line 22 is Graph graph = new Graph(); and line 33 is graph.addNode(tempNode);

What do I wrong?

Graph graph = new Graph();
no pointer with new again.

line 33 is graph.addNode(tempNode);
most likely, the compiler is confused by the first error and now cannot determine what to do with "graph"

9. Registered User
Join Date
Nov 2008
Posts
37
I changed it to Graph *graph = new Graph(); but now I get new errors:
Code:
```../main.cpp: In function 'int main()':
../main.cpp:33: error: request for member 'addNode' in 'graph', which is of non-class type 'Graph*'
../main.cpp:37: error: request for member 'getNumNodes' in 'graph', which is of non-class type 'Graph*'
../main.cpp:38: error: request for member 'getNumNodes' in 'graph', which is of non-class type 'Graph*'
../main.cpp:39: error: request for member 'getNodeAt' in 'graph', which is of non-class type 'Graph*'
../main.cpp:39: error: request for member 'getNodeAt' in 'graph', which is of non-class type 'Graph*'
../main.cpp:40: error: request for member 'getNodeAt' in 'graph', which is of non-class type 'Graph*'
../main.cpp:40: error: request for member 'getNodeAt' in 'graph', which is of non-class type 'Graph*'
../main.cpp:46: error: request for member 'print' in 'graph', which is of non-class type 'Graph*'```
How can I fix the errors?

10. Senior Member
Join Date
Dec 2003
Posts
3,366
You are taking on too much, you do not appear to understand pointers or basics and are trying graph theory problems --- some of the most aggravating problems in computer science. You probably should drop down, do a simple pointer program (like a linked list maybe?) before you continue, in my opinion.

Anyway, this is your problem:

class whateva
{
public:
int stuff;
double data;
void foo();
...
};

whateva* pwe = new whatever;
whateva noptr;

noptr.foo(); //OK, not a pointer.
pwe.foo(); //error: pointers do not have functions.
pwe->foo(); //OK, the thing POINTED to by the pointer DOES have a function in this case.

also, the archaic C style version works:
*pwe.foo(); //OK, the thing pointed to is extracted before the member is invoked.
///however, this gets confusing in some code, imagine math code where you have

x = *a.x* *b.t; ///wow, thats hard to read!

finally this also works:

pwe[0].foo(); //any pointer can be treated like an array! generally, if you did not use [] in the new statement to create many items, you would not, but you *can* do it with zero as the offset, since you have one item an [0] is the first item.

11. Registered User
Join Date
Nov 2008
Posts
37
I understand, but I have two classes:
Code:
```class GraphNode {
public:
GraphNode();
GraphNode(int label, double x, double y, double z);
};```
Code:
```class  Graph {
private:
vector<GraphNode> nodes;
public:
};```
Code:
```int main() {
Graph graph = new Graph();
GraphNode *tempNode = new GraphNode(s_nodeTmp.label,....
}```
But how works pointer with two encapsulated classes?

12. Senior Member
Join Date
Dec 2003
Posts
3,366
Originally Posted by flaxyz
I understand, but I have two classes:
Code:
```class GraphNode {
public:
GraphNode();
GraphNode(int label, double x, double y, double z);
};```
Code:
```class  Graph {
private:
vector<GraphNode> nodes;
public:
};```
Code:
```int main() {
Graph graph = new Graph();
GraphNode *tempNode = new GraphNode(s_nodeTmp.label,....
}```
But how works pointer with two encapsulated classes?
Pointers work pretty much the same way for everything, whether its a class or a byte or anything in between, with a few oddities like function pointers that we really, really do not need to throw into the mix today. The problem you have is bad syntax, you are mixing pointer and non pointer syntax. If the variable is a pointer and a class, use one of the 3 methods I listed for pointer access. If its not a pointer, you use the field operator (.). The compiler is trying to tell you this, but it takes some practice to understand compiler messages.

13. Registered User
Join Date
Nov 2008
Posts
37
OK, I can compile the code now, but I still do not understande how can I compute the distances between nodes.

Here is the output of the program:
Code:
```	--- Node 39 ---
Coordinate:		18.378, 59.466, 13.185
Adjacent Nodes:	40, 41, 43, 44, 45, 48, 49

--- Node 40 ---
Coordinate:		14.27, 54.556, 18.227
Adjacent Nodes:	39, 41, 43, 44, 45, 48, 49

--- Node 41 ---
Coordinate:		30.119, 51.333, 33.732
Adjacent Nodes:	39, 40, 43, 44, 45, 48, 49

--- Node 43 ---
Coordinate:		26.326, 51.1, 21.154
Adjacent Nodes:	39, 40, 41, 44, 45, 48, 49

--- Node 44 ---
Coordinate:		23.764, 55.05, 18.338
Adjacent Nodes:	39, 40, 41, 43, 45, 48, 49

--- Node 45 ---
Coordinate:		17.697, 75.884, 26.178
Adjacent Nodes:	39, 40, 41, 43, 44, 48, 49

--- Node 48 ---
Coordinate:		22.351, 66.269, 28.727
Adjacent Nodes:	39, 40, 41, 43, 44, 45, 49

--- Node 49 ---
Coordinate:		19.98, 63.046, 33.535
Adjacent Nodes:	39, 40, 41, 43, 44, 45, 48```
Input file
Code:
```39      18.378  59.466  13.185
40      14.270  54.556  18.227
41      30.119  51.333  33.732
43      26.326  51.100  21.154
44      23.764  55.050  18.338
45      17.697  75.884  26.178
48      22.351  66.269  28.727
49      19.980  63.046  33.535```
main.cpp
Code:
```#include "graph.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct s_node {
int label;
double x, y, z;
};

istream &operator>>(istream &in, s_node &n) {
return in >> n.label >> n.x >> n.y >> n.z;
}

int main() {
//make an empty graph, and an empty temporary graph node
Graph *graph = new Graph();
s_node s_nodeTmp;

ifstream in("template.txt");
//construct the nodes
while(in >> s_nodeTmp) {
//GraphNode *tempNode = new GraphNode(s_nodeTmp.label,
GraphNode tempNode (s_nodeTmp.label,
s_nodeTmp.x,
s_nodeTmp.y,
s_nodeTmp.z);
//delete tempNode;
}
for (size_t i = 0; i < graph->getNumNodes(); i++) {
for (size_t j = 0; j < graph->getNumNodes(); j++) {
if (graph->getNodeAt(i).getLabel() != graph->getNodeAt(j).getLabel()){
}
}
}

//print the graph
graph->print();
delete graph;
return 0;
}```
graph.h
Code:
```#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class GraphNode
{
private:
vector<int> adjacencyList; //list of indices of nodes connected to this node
int label;
double x,y,z; //x,y,z as in a point in 3-d space.

public:
GraphNode(int label, double x, double y, double z);

//get and set functions

void setLabel(int label);
void setX(double x);
void setY(double y);
void setZ(double z);

int getLabel();
double getX();
double getY();
double getZ();

//utility functions
void print();
void distance();
};

class Graph
{
private:
vector<GraphNode> nodes;  //an array of nodes to represent the graph
vector<vector<double> > distance_matrix;
vector<vector<int> > connects;

public:
//get functions
size_t getNumNodes();

//utility functions
void print();
GraphNode& getNodeAt(size_t index);
void setNodeAt(size_t index, GraphNode node);

};

#endif /* GRAPH_H_ */```
graph.cpp
Code:
```#include "graph.h"

using namespace std;

GraphNode::GraphNode(int label, double x, double y, double z){
setLabel(label); setX(x); setY(y); setZ(z);
}

{
//returns the complete adjacency list
}

{
//returns the number of adjacent nodes
}

void GraphNode::setLabel(int label){this->label = label;}
void GraphNode::setX(double x){this->x = x;}
void GraphNode::setY(double y){this->y = y;}
void GraphNode::setZ(double z){this->z = z;}

int GraphNode::getLabel(){return label;}
double GraphNode::getX(){return x;}
double GraphNode::getY(){return y;}
double GraphNode::getZ(){return z;}

}

void GraphNode::print()
{
cout << "\t--- Node " << label << " ---"<<endl;
cout << "Coordinate:\t\t"
<< getX() << ", "
<< getY() << ", "
<< getZ() << " "
<< endl;
cout << "Adjacent Nodes:\t";
for (size_t count = 0; count < adjacencyList.size(); count++)
{
if (count != adjacencyList.size() - 1)
{
cout << ", ";
}
}
cout << endl;
}

void GraphNode::distance(){
double x_p, y_p, z_p;

/*	for(int i= 1; i <= nodeList.size(); ++i)
{
for(int j = 1; j <= nodeList.size(); ++j)
{
if (i == j)
{
distance_matrix[i][j] = 0;
}
else
{
x_p = nodeList[i].getX() - nodeList[j].getX();
y_p = nodeList[i].getY() - nodeList[j].getY();
z_p = nodeList[i].getZ() - nodeList[j].getZ();
this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p );
}
}
}*/
}

size_t Graph::getNumNodes()
{
//returns the number of nodes in the graph
return nodes.size();
}

{
nodes.push_back(node);
}

GraphNode& Graph::getNodeAt(size_t index)
{
//if ((index < numNodes) && (index >= 0))
if ((index < nodes.size()) && (index >= 0))
{
return nodes[index];
}
else
{
//invalid index specified
//		return GraphNode();  //return an 'empty' node
}
}

void Graph::print()
{
for (size_t count = 0; count < nodes.size(); count++)
{
nodes[count].print();
cout << endl << endl;
}
}

void Graph::setNodeAt(size_t index, GraphNode node)
{
//assignments are only allowed into existing locations
if ((index < nodes.size()) && (index >= 0))
{
nodes[index] = node;
}
}```
Unfortunately, I do not know how can I get access from the GraphNode class to another GraphNodes in order to get the coordinates and compute the distances between adjacent nodes?
Last edited by flaxyz; 05-21-2009 at 08:20 PM.

14. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
Your distances() function should be a function of the graph not of a node. Your graph can retrieve the nodes because it can use the getNodeAt() function.

Code:
```void Graph::distances()
{
double x_p, y_p, z_p;

for(int i= 0; i < nodes.size(); ++i)
{
for(int j = 0; j < nodes.size(); ++j)
{
if (i == j)
{
distance_matrix[i][j] = 0;
}
else
{
GraphNode iNode = getNodeAt(i);
GraphNode jNode = getNodeAt(j);
x_p = iNode.getX() - jNode.getX();
y_p = iNode.getY() - jNode.getY();
z_p = iNode.getZ() - jNode.getZ();
this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p );
}
}
}```
}

This will populate the distance_matrix and then you can look up and take the square root in order to get the distance.

15. Registered User
Join Date
Nov 2008
Posts
37
Thank you it works. The output looks like:
Code:
```0 66.4055 626.176 196.666 75.0635 438.833 303.619 429.505
66.4055 0 501.984 165.858 90.3924 529.846 312.747 339.019
626.176 501.984 0 172.647 291.177 814.121 308.476 240.032
196.666 165.858 172.647 0 30.0962 713.947 303.25 336.268
75.0635 90.3924 291.177 30.0962 0 532.33 235.794 309.203
438.833 529.846 814.121 713.947 532.33 0 120.605 224.152
303.619 312.747 308.476 303.25 235.794 120.605 0 39.1262
429.505 339.019 240.032 336.268 309.203 224.152 39.1262 0```
How can I modified the output that looks like a matrix i.e. I mean to change the space between the values?

I put additionally in the distance() function this:
Code:
```	distanceMatrix.resize(nodes.size());
for (size_t i = 0; i < nodes.size(); i++)
distanceMatrix[i].resize(nodes.size());```
I call the distance() function from main.cpp like
Code:
```	graph->distance();
graph->distanceMatrixOutput();```
I am no that it is all correct or no?

Here is the new code. main.cpp
Code:
```#include "graph.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct s_node {
int label;
double x, y, z;
};

istream &operator>>(istream &in, s_node &n) {
return in >> n.label >> n.x >> n.y >> n.z;
}

int main() {
//make an empty graph, and an empty temporary graph node
Graph *graph = new Graph();
s_node s_nodeTmp;

ifstream in("template.txt");
//construct the nodes
while(in >> s_nodeTmp) {
//GraphNode *tempNode = new GraphNode(s_nodeTmp.label,
GraphNode tempNode (s_nodeTmp.label,
s_nodeTmp.x,
s_nodeTmp.y,
s_nodeTmp.z);
//delete tempNode;
}
for (size_t i = 0; i < graph->getNumNodes(); i++) {
for (size_t j = 0; j < graph->getNumNodes(); j++) {
if (graph->getNodeAt(i).getLabel() != graph->getNodeAt(j).getLabel()){
}
}
}

//print the graph
graph->print();
graph->distance();
graph->distanceMatrixOutput();
delete graph;
return 0;
}```
graph.h
Code:
```#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class GraphNode
{
private:
vector<int> adjacencyList; //list of indices of nodes connected to this node
int label;
double x,y,z; //x,y,z as in a point in 3-d space.

public:
GraphNode(int label, double x, double y, double z);

//get and set functions

void setLabel(int label);
void setX(double x);
void setY(double y);
void setZ(double z);

int getLabel();
double getX();
double getY();
double getZ();

//utility functions
void print();
};

class Graph
{
private:
vector<GraphNode> nodes;  //an array of nodes to represent the graph
vector<vector<double> > distanceMatrix;
vector<vector<int> > connects;

public:
//get functions
size_t getNumNodes();

//utility functions
void print();
GraphNode& getNodeAt(size_t index);
void setNodeAt(size_t index, GraphNode node);
void distance();
void distanceMatrixOutput();
};

#endif /* GRAPH_H_ */```
graph.cpp
Code:
```#include "graph.h"

using namespace std;

GraphNode::GraphNode(int label, double x, double y, double z){
setLabel(label); setX(x); setY(y); setZ(z);
}

{
//returns the complete adjacency list
}

{
//returns the number of adjacent nodes
}

void GraphNode::setLabel(int label){this->label = label;}
void GraphNode::setX(double x){this->x = x;}
void GraphNode::setY(double y){this->y = y;}
void GraphNode::setZ(double z){this->z = z;}

int GraphNode::getLabel(){return label;}
double GraphNode::getX(){return x;}
double GraphNode::getY(){return y;}
double GraphNode::getZ(){return z;}

}

void GraphNode::print()
{
cout << "\t--- Node " << label << " ---"<<endl;
cout << "Coordinate:\t\t"
<< getX() << ", "
<< getY() << ", "
<< getZ() << " "
<< endl;
cout << "Adjacent Nodes:\t";
for (size_t count = 0; count < adjacencyList.size(); count++)
{
if (count != adjacencyList.size() - 1)
{
cout << ", ";
}
}
cout << endl;
}

size_t Graph::getNumNodes()
{
//returns the number of nodes in the graph
return nodes.size();
}

{
nodes.push_back(node);
}

GraphNode& Graph::getNodeAt(size_t index)
{
//if ((index < numNodes) && (index >= 0))
if ((index < nodes.size()) && (index >= 0))
{
return nodes[index];
}
else
{
//invalid index specified
//		return GraphNode();  //return an 'empty' node
}
}

void Graph::print()
{
for (size_t count = 0; count < nodes.size(); count++)
{
nodes[count].print();
cout << endl << endl;
}
}

void Graph::setNodeAt(size_t index, GraphNode node)
{
//assignments are only allowed into existing locations
if ((index < nodes.size()) && (index >= 0))
{
nodes[index] = node;
}
}

void Graph::distance()
{
double x_p, y_p, z_p;
//resize a distance matrix
distanceMatrix.resize(nodes.size());
for (size_t i = 0; i < nodes.size(); i++)
distanceMatrix[i].resize(nodes.size());

for(size_t i= 0; i < nodes.size(); ++i)
{
for(size_t j = 0; j < nodes.size(); ++j)
{
if (i == j)
{
distanceMatrix[i][j] = 0;
}
else
{
GraphNode iNode = getNodeAt(i);
GraphNode jNode = getNodeAt(j);
x_p = iNode.getX() - jNode.getX();
y_p = iNode.getY() - jNode.getY();
z_p = iNode.getZ() - jNode.getZ();
distanceMatrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p );
}
}
}
}

void Graph::distanceMatrixOutput() {
cout << "distance matrix output" << endl;
for(size_t i=0; i<distanceMatrix.size(); i++)
{
for(size_t j=0; j<distanceMatrix[i].size(); j++)
{
cout << distanceMatrix[i][j] << " ";
}
cout << '\n';
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source