Jump to content
  • Advertisement
Sign in to follow this  
Sagar_Indurkhya

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
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...


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!