# Trouble with all these classes! - C++

This topic is 4950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

EDIT- Changed Code post I am having trouble with linking together some classes. Basically I have 3 classes: - Neuron - NeuralNetworkLayer - NeuralNetwork NeuralNetwork has #include "NeuralNetworkLayer.h", so that is fine NeuralNetworkLayer has #include "Neuron.h" so that is fine However, Neuron uses NeuralNetworkLayer too, so I tried doing #include "NeuralNetworkLayer.h" and it spit out the errors: Neuron.h(42): error C2027: use of undefined type 'NeuralNetworkLayer' Neuron.h(50): error C2027: use of undefined type 'NeuralNetworkLayer' Neuron.h(86): error C2027: use of undefined type 'NeuralNetworkLayer' Neuron.h(86): error C2227: left of '->neurons' must point to class/struct/union Neuron.h(42): error C2227: left of '->numNeurons' must point to class/struct/union Neuron.h(50): error C2227: left of '->numNeurons' must point to class/struct/union Here is Neuron.h:
#pragma once
#include "stdafx.h"
#include <stdlib.h>
#include <math.h>

class NeuralNetworkLayer;

class Neuron
{
public:

// Public Member Variables
double* weights;
int NumOfInputs;
NeuralNetworkLayer *previousLayer; // ERROR
NeuralNetworkLayer *nextLayer; // ERROR
double output;
const double E;
int NeuronState;

Neuron(void) : E(2.7182818284590452354)
{
}

Neuron(int state, NeuralNetworkLayer* prevLayer,  NeuralNetworkLayer* nxtLayer) : E(2.7182818284590452354)
{
NeuronState = state;

switch(state)
{
case 0:
{
break;
}
case 1:
{
previousLayer = prevLayer;
nextLayer = nxtLayer;

// Save the number of input neurons
NumOfInputs = previousLayer->numNeurons;
break;
}
case 2:
{
previousLayer = prevLayer;

// Save the number of input neurons
NumOfInputs = previousLayer->numNeurons;
break;
}
}

weights = new double[NumOfInputs];

for(int i = 0; i < NumOfInputs; i++)
weights =  (double)rand() / (double)(RAND_MAX+1);

output = 0.0f;
}

~Neuron(void)
{

}

void setInput(double x)
{
output = x;
}

void BackPropagateError(double correctOutput)
{
double MSE = pow(correctOutput - output, 2); // Mean Square Error
}

private:

void CalculateOutput()
{
/* WARNING: DO NOT USE THIS FUNCTION IF THIS IS A INPUT NEURON */
output = 0.0f;

for(int i = 0; i < NumOfInputs; i++)
output += weights * (*previousLayer->neurons);

output = Sigmoid_Function(output);
}

inline double Sigmoid_Function(double x)
{
// Calculate Sigmoid Function here
return (double)1/((double)1 + pow(E,x));
}
};

This particular problem has bit me before, and it really annoys me! What should I do?!? [Edited by - Sagar_Indurkhya on December 31, 2004 5:26:57 PM]

##### Share on other sites
You probably want to forward declare NeuralNetworkLayer, so place 'class NeuralNetworkLayer;' instead of '#include "NeuralNetworkLayer.h' in Neuron.h
Note that you can do this because you are using pointers to NeuralNetworkLayer objects -- and not the objects themselves -- in your Neuron class.

Regards,
jflanglois

##### Share on other sites
Whoops. I posted the wrong code. in Neuron.h, I used
class NeuralNetworkLayer; and those previous errors are the ones I got.

##### Share on other sites
Forward-declaring the "NeuralNetworkLayer" class only allows class Neuron to know that such things exist; at this point nothing is known about the NNL class interface nor the size of NNL objects. Therefore you can't call any methods via the NNL pointers yet.

What you should do is put the implementation code into Neuron.cpp, which will include both Neuron.h and NeuralNetworkLayer.h .

And of course, the obligatory link for all such matters...

##### Share on other sites
What i usually do in this case is have one class include the other and do all the other classes operations for it

##### Share on other sites
Quote:
 Original post by ZahlmanForward-declaring the "NeuralNetworkLayer" class only allows class Neuron to know that such things exist; at this point nothing is known about the NNL class interface nor the size of NNL objects. Therefore you can't call any methods via the NNL pointers yet.What you should do is put the implementation code into Neuron.cpp, which will include both Neuron.h and NeuralNetworkLayer.h .And of course, the obligatory link for all such matters...

Ah, right. I hadn't looked through all the code. Thanks for pointing it out.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002234
×