Hi, I'm new here, I hope I posted this in the correct forum.
I want to describe a network of nodes. Each node needs to connect to all its neighbours. Therefore, in order to be able to do some calculations, I need to access the properties of each of the neighbours of the node that I am currently working on. I decided to set this up with a vector, which is new to me. My code compiles just fine, but when I run it, the program just crashes. I THINK that I am doing something wrong in the iterator arithmetic, I THINK that I am messing about with incompatible types; but I can't seem to figure out how to fix it; the fixes I tried didn't compile and gave me error messages that didn't help me. I'll provide snippets from my code, and I would be thankful if anybody would point me in the right direction. (NOTE: there are more functions and there is more code in the program, but I think I have the relevant part in this post. If you suspect differently, please tell me so!)
In node.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#ifndef NODE_H
#define NODE_H
#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
class Node
{
public:
Node(); //default
Node(double set_x, double set_y, int set_RowNumber); //argument ctor
double Get_x() { return x; }
void Set_x(double val) { x = val; }
int Get_RowNumber() { return RowNumber; }
void Set_RowNumber(int val) { RowNumber = val; }
double Get_y() { return y; }
void Set_y(double val) { y = val; }
double Get_D(int i) {return D[i]; }
void Set_D(int i, double val) { D[i] = val; }
double Get_H(int i) { return H[i]; }
void Set_H(int i, double val) { H[i] = val; }
double Get_alpha(int i) { return alpha[i]; }
void Set_alpha(int i, double val) { alpha[i] = val; }
protected:
private:
double x;
double y;
int RowNumber;
double D[6];
double H[6];
double alpha[12];
};
class Network
{
public:
Network(int set_NumRows, int set_NumNodesInRow, double *xval, double *yval, int *RowNumberval); //ctor
Node NodeIndex(int index) { return nodelist.at(index); }
void CalculateFacets() ;
int GetNumNodes() { return NumNodes; }
int GetNumNodesInRow() { return NumNodesInRow; }
int GetNumRows() { return NumRows; }
private:
std::vector<Node> nodelist;
int NumRows;
int NumNodesInRow;
int NumNodes;
};
#endif // NODE_H
|
and from node.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include ".\\include\\Node.h"
Node::Node(double set_x, double set_y, int set_RowNumber)
{
x = set_x;
y = set_y;
RowNumber = set_RowNumber;
for (int i = 0 ; i <12; i++)
{
if (i < 6)
{
D[i] = 0;
H[i] = 0;
}
alpha[i] = 0;
}
}
Network::Network(int set_NumRows, int set_NumNodesInRow, double *xval, double *yval, int *RowNumberval)
{
int NumRows = set_NumRows;
int NumNodesInRow = set_NumNodesInRow;
int NumNodes = NumRows*NumNodesInRow;
nodelist.resize(NumNodes);
for (int i = 0; i<NumNodes; i++)
{
this->nodelist.at(i).Set_x(*(xval+i));
this->nodelist.at(i).Set_y(*(yval+i));
this->nodelist.at(i).Set_RowNumber(*(RowNumberval+i));
}
}
void Network::CalculateFacets()
{
int Ntot, Nperrow;
Ntot = this->GetNumNodes();
Nperrow = this->GetNumNodesInRow();
int RowCounter = 0;
int NodeCounter = 0;
for (vector<Node>::iterator i = this->nodelist.begin();
i != (this->nodelist.end()-Nperrow);
i++) //ignore last row
{
RowCounter = (*i).Get_RowNumber();
cout << "RowCounter = "<<RowCounter<<endl;
cout << "y i = "<< ( (*i).Get_y() )<<endl;
cout << "x i = "<< ( (*i).Get_x() )<<endl;
//program outputs correct values for RowCounter, x and y for the first node, then crashes, so the bug manifests itself in the next line:
cout << "x i+Nperrow = " << ((*(i + Nperrow)).Get_x())<<endl;
(*i).Set_D(1,( (*(i+Nperrow)).Get_x() - (*i).Get_x() ));
cout << (*i).Get_D(1);
}
|
PS: I didn't post main.cpp as I think it isn't relevant, I think the mistake is in how I reference node # (i+Nperrow); it would take quite a bit of "snipping" to present the relevant bit of the main code; I could do it if it is required for the solution.
PS2: I tried making Nperrow of type iterator, but clearly I am using an incorrect syntax, since all my attempts at that have resulted in compiler errors. Is that even what is wrong with the code?
PS3: thanks in advance!