My friend, you are suffering through pointer hell. The best way to avoid this is to use the standard template library. Vectors are infinitely nicer than dynamically allocated arrays of pointers. In order to understand your code, I had to work through it. In the process, i utilized the standard template library. The code worked almost immediately after eliminating compiler errors. Here it is:
#include <iostream>
#include <vector>
class neuron
{
public:
// Other members and constructors omitted
std::vector<neuron*> connections;
void connect( const std::vector<neuron*>& inconnections )
{
connections.clear();
for( unsigned int i=0; i<inconnections.size(); i++ )
{
if( this == inconnections[i] )
continue;
connections.push_back( inconnections[i] );
}
}
//Printing functions ommitted
};
class network
{
public:
std::vector<neuron*> neurons;
neuron*& slot( int i, int j )
{
return neurons[ i * w + j ];
}
network( int w, int h ): w(w), h(h)
{
neurons = std::vector<neuron*>( w * h, NULL );
for( int i=0; i<h; i++ )
{
for( int j=0; j<w; j++ )
{
slot( i, j ) = new neuron( i, j );
}
}
for( unsigned int i=0; i<neurons.size(); i++ )
neurons[i]->connect( neurons );
}
// Destructor and printing functions ommitted
};
using namespace std;
int main()
{
network net(2,2);
net.printNetwork();
return 0;
}
Outut:
Network structure:
-------------------
My name is: [0,0]
I am connected to:
0,1
1,0
1,1
My name is: [0,1]
I am connected to:
0,0
1,0
1,1
My name is: [1,0]
I am connected to:
0,0
0,1
1,1
My name is: [1,1] …