Ayuda herencia clases
Publicado por Aitor Ruano Miralles (1 intervención) el 05/07/2009 19:07:53
Muy buenas a todos,
Estoy desarrollando una librerías en C++ para hacer más fácil la implementación de redes neuronales en este lenguaje, pero tengo un problema con la herencia de clases, el hecho es que al intentar heredar una clase llamada "perceptron" de un clase llamada "neurona" g++ me devuelve este error en todas las definiciones de los métodos de "peceptron" así como los constructores: "error: no se pueden definir tipos nuevos en una devolución de tipo".
Os pego aquí el código para que podáis examinarlo, son dos cabeceras, neuron.h y perceptron.h:
neuron.h:
#include <math.h> //This is to do some mathematical stuff such as exp, sin, sqrt...
//#include "round.h" //To round double numbers to certain decimals...
#define sigmoid (1)/(1 + exp(-propagation)) //These are definitions of the sigmoidal function to get an analogic output, this make easier to read the code.
#define dsigmoid ((sigmoid)*(1 - (sigmoid)))
//This is a definition of a class called neuron, it contains some of its features, such as inputs, weights, output and functions to acces and change them.
class neuron
{
protected:
double *inputs; //Entries for the neuron, it'll be a dinamic array
double *weights; //Weights for the connectios, it'll be a dinamic array
double propagation; //To save the propagation of the neuron
double output; //To save its output
double error; //To save its error
int connections; //To know the number of connections, it would be possibly to know counting the number of elements in the "inputs" array, but it's impossible
//because it's save dinamically
public:
//Some function declartions.
void set_input(int input, double input_val); //Set an input for a neuron
double const get_input(int input); //Get an input for a neuron
void set_weight(int weight, double weight_val); //Set a weight for a neuron
double const get_weight(int weight); //Get a weight for a neuron
double const get_propagation(); //Get the propagation of a neuron
double const get_output(); //Get the output of a neuron
double get_error(double expected); //Get the error of a neuron.
//Constructor and destructor of the class
neuron(){}
~neuron(){
delete [] inputs;
delete [] weights;} //Destructor
};
void neuron::set_input(int input, double input_val)
{
inputs[input] = input_val;
}
double const neuron::get_input(int input)
{
return inputs[input];
}
void neuron::set_weight(int weight, double weight_val)
{
weights[weight] = weight_val;
}
double const neuron::get_weight(int weight)
{
return weights[weight];
}
//----------------------------------------------------------------------------------------
double const neuron::get_propagation()
{
//compute();
return propagation;
}
double const neuron::get_output()
{
//compute();
return output;
}
/*double neuron::get_error(double expected)
{
compute();
error = expected - output;
return error;
}*/
perceptron.h:
#include "neuron.h"
class perceptron: public neuron
{
private:
double b;
public:
void compute();
//Constructor and destructor of the class
perceptron(int _connections); //Creates a neuron with default values and allow the user to set a number of inputs
perceptron(double _inputs[], double _weights[]); //This one is to specify the values of the entries and the weights
~perceptron(){} //Destructor
}
perceptron::perceptron(int _connections): neuron()
{
connections = _connections;
inputs = new double[connections]; //Creates the dinamic array (with the number of "connections")
weights = new double[connections];
int i; //For bucle, to set all the array "boxes" to zero
for (i = 0; i < connections; i++)
{
weights[i], inputs[i] = 0;
}
//Default values
propagation = 0;
error = 0;
output = 0;
b = 0.5;
}
perceptron::perceptron(double _inputs[], double _weights[]): neuron() //The same as the previous but with the arguments specified
{
connections = sizeof(*_inputs);
inputs = new double[connections];
weights = new double[connections];
int i;
for (i = 0; i < connections; i++)
{
inputs[i] = _inputs[i];
weights[i] = _weights[i];
}
propagation = 0;
error = 0;
output = 0;
b = 0.5;
}
void perceptron::compute()
{
propagation = 0;
int i = 0;
for (i = 0; i < connections; i++) //This bucle sums the product of every input by its weight
{
propagation = propagation + (inputs[i])*(weights[i]) + b;
}
if (propagation > 0)
{
output = 1; //hardlim func. it gives a 1 to the exit if the output is > 0 and a 0 if its <.
}
else
{
output = 0;
}
}
Espero una ayuda pronto, gracias ;)
Estoy desarrollando una librerías en C++ para hacer más fácil la implementación de redes neuronales en este lenguaje, pero tengo un problema con la herencia de clases, el hecho es que al intentar heredar una clase llamada "perceptron" de un clase llamada "neurona" g++ me devuelve este error en todas las definiciones de los métodos de "peceptron" así como los constructores: "error: no se pueden definir tipos nuevos en una devolución de tipo".
Os pego aquí el código para que podáis examinarlo, son dos cabeceras, neuron.h y perceptron.h:
neuron.h:
#include <math.h> //This is to do some mathematical stuff such as exp, sin, sqrt...
//#include "round.h" //To round double numbers to certain decimals...
#define sigmoid (1)/(1 + exp(-propagation)) //These are definitions of the sigmoidal function to get an analogic output, this make easier to read the code.
#define dsigmoid ((sigmoid)*(1 - (sigmoid)))
//This is a definition of a class called neuron, it contains some of its features, such as inputs, weights, output and functions to acces and change them.
class neuron
{
protected:
double *inputs; //Entries for the neuron, it'll be a dinamic array
double *weights; //Weights for the connectios, it'll be a dinamic array
double propagation; //To save the propagation of the neuron
double output; //To save its output
double error; //To save its error
int connections; //To know the number of connections, it would be possibly to know counting the number of elements in the "inputs" array, but it's impossible
//because it's save dinamically
public:
//Some function declartions.
void set_input(int input, double input_val); //Set an input for a neuron
double const get_input(int input); //Get an input for a neuron
void set_weight(int weight, double weight_val); //Set a weight for a neuron
double const get_weight(int weight); //Get a weight for a neuron
double const get_propagation(); //Get the propagation of a neuron
double const get_output(); //Get the output of a neuron
double get_error(double expected); //Get the error of a neuron.
//Constructor and destructor of the class
neuron(){}
~neuron(){
delete [] inputs;
delete [] weights;} //Destructor
};
void neuron::set_input(int input, double input_val)
{
inputs[input] = input_val;
}
double const neuron::get_input(int input)
{
return inputs[input];
}
void neuron::set_weight(int weight, double weight_val)
{
weights[weight] = weight_val;
}
double const neuron::get_weight(int weight)
{
return weights[weight];
}
//----------------------------------------------------------------------------------------
double const neuron::get_propagation()
{
//compute();
return propagation;
}
double const neuron::get_output()
{
//compute();
return output;
}
/*double neuron::get_error(double expected)
{
compute();
error = expected - output;
return error;
}*/
perceptron.h:
#include "neuron.h"
class perceptron: public neuron
{
private:
double b;
public:
void compute();
//Constructor and destructor of the class
perceptron(int _connections); //Creates a neuron with default values and allow the user to set a number of inputs
perceptron(double _inputs[], double _weights[]); //This one is to specify the values of the entries and the weights
~perceptron(){} //Destructor
}
perceptron::perceptron(int _connections): neuron()
{
connections = _connections;
inputs = new double[connections]; //Creates the dinamic array (with the number of "connections")
weights = new double[connections];
int i; //For bucle, to set all the array "boxes" to zero
for (i = 0; i < connections; i++)
{
weights[i], inputs[i] = 0;
}
//Default values
propagation = 0;
error = 0;
output = 0;
b = 0.5;
}
perceptron::perceptron(double _inputs[], double _weights[]): neuron() //The same as the previous but with the arguments specified
{
connections = sizeof(*_inputs);
inputs = new double[connections];
weights = new double[connections];
int i;
for (i = 0; i < connections; i++)
{
inputs[i] = _inputs[i];
weights[i] = _weights[i];
}
propagation = 0;
error = 0;
output = 0;
b = 0.5;
}
void perceptron::compute()
{
propagation = 0;
int i = 0;
for (i = 0; i < connections; i++) //This bucle sums the product of every input by its weight
{
propagation = propagation + (inputs[i])*(weights[i]) + b;
}
if (propagation > 0)
{
output = 1; //hardlim func. it gives a 1 to the exit if the output is > 0 and a 0 if its <.
}
else
{
output = 0;
}
}
Espero una ayuda pronto, gracias ;)
Valora esta pregunta
0