Perhaps I will have my problem restated:
All the public member (including 'neighbor') is
working well in the constructor. I can access them freely.
But when thing go into the main function, 'neighbor' and
only 'neighbor' cannot be accessed, others (like 'conn') have no problem.
I have made some change to let the problem more clear.
The main function is now
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <iomanip>
using namespace std;
#include "createnetwork.h"
int main()
{
int N[1]={2};
int arr1[3]={0,1,1};
int arr2[3]={2,0,0};
network network1(1,N,arr1,arr2);
cout << network1.n << endl;
for (int i=0;i<network1.n;i++)
cout << network1.conn[i] << " ";
cout << endl;
cout << network1.neighbor[0];
cout << endl;
}
|
Here's some background infomation:
I am major in Physics and I am studying network problem. What I do here is to create a specific network. The nodes can be divided into layers and nodes within same layer form a ring-like structure and
node[i] gives the number of nodes in each layer.
Node 0 is the center of the network and all the 'rings'. Between adjacent layers, there are some links connecting them.
So in[i] represents the number of links going towards center of node i and out[i] is the no of links going out.
The meaning of each variable:
1 2 3 4 5 6 7 8 9
|
int *cumul; //cumul[n]: The number of nodes in the first n layers
//n=cumul[layer]: Total no of nodes in the network
int *ins; //cumulative inwards links
int *outs; //cumulative outwards links
struct link *link;
int n; //number of nodes
int *conn; //conn[i]: connectivity of node i
int *N; //N[i]: sum of connectivity of nodes with label<i
int *neighbor; //neighbor[N[i]+(j-1)]: the j-th neighbor of node i
|
I hope the description may help understanding my code.
So here's the whole constructor:
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
network::network (int layer, int node[],int in[],int out[])
{
cumul=new int [layer+1];
cumul[0]=1;
for (int i=1;i<=layer;i++)
cumul[i]=cumul[i-1]+node[i-1];
n=cumul[layer];
ins=new int [n];
outs=new int [n];
ins[0]=in[0];
outs[0]=out[0];
for (int i=1;i<n;i++)
ins[i]=ins[i-1]+in[i];
for (int i=1;i<n;i++)
outs[i]=outs[i-1]+out[i];
link = new struct link [ins[n-1]];
int a=0;
int b=0;
int c=0;
//see the excel file for the assignment of links
for (int i=0;i<ins[n-1];i++)
{
while (outs[a]<=i)
a++;
link[i].a=a;
if (i==ins[cumul[c]-1])
{
c++;
link[i].b=cumul[c]-1;
}
else
{
while (ins[b]<i)
b++;
link[i].b=b;
}
}
cout << "links found\n";
conn = new int [n];
conn[0]=0;
for (int i=1;i<n;i++)
conn[i]=2;
for (int i=0;i<ins[n-1];i++)
{
conn[link[i].a]++;
conn[link[i].b]++;
}
cout << "connectivity found\n";
N=new int [n+1];
N[0]=0;
for (int i=1;i<=n;i++)
N[i]=N[i-1]+conn[i-1];
int *neighbor;
neighbor = new int [N[n]];
for (int i=0;i<N[n];i++)
neighbor[i] = -1;
c=0;
for (int i=1;i<n;i++)
{
if (i==cumul[c])
neighbor[N[i+1]-1]=cumul[++c]-1;
else
neighbor[N[i+1]-1]=i-1;
}
c=layer;
for (int i=n-1;i>0;i--)
{
if (i==cumul[c]-1)
neighbor[N[i+1]-2]=cumul[--c];
else
neighbor[N[i+1]-2]=i+1;
}
for (int i=0;i<ins[n-1];i++)
{
a=N[link[i].a];
while (neighbor[a]!=-1)
a++;
neighbor[a]=link[i].b;
b=N[link[i].b];
while (neighbor[b]!=-1)
b++;
neighbor[b]=link[i].a;
}
cout << "network created\n";
cout << n << endl;
for (int i=0;i<n;i++)
cout << conn[i] << " ";
cout << endl;
cout << neighbor[0] << endl << endl;
}
|
And the output window goes like this:
1 2 3 4 5 6 7 8 9
|
links found
connectivity found
network created
3 //n in the constructor
2 3 3 //conn in the constructor
2 //neighbor[0] in the constructor
3 //network1.n in the main function
2 3 3 //network1.conn in the main function
|
And then it gets stuck.
Everything is initialized in the constructor (able to return me the appropriate value)
But when things go to the main function, others still works well but not the neighbor array.
In fact, the whole thing will work well if I copy the whole thing to the main function, just as it works in the constructor. Indeed, I first write the code in the main function and then copy it to the constructor. I tested it with a number of parameters and it works. So I do not think that there is problem in the constructor. However, something got wrong now. Is that some underlying rules for defining class that I violate?