我的第一个使用类变量和多态的C ++项目

时间:2016-01-24 19:11:01

标签: c++ inheritance polymorphism

我在这里阅读了很多帖子和一些教程,尝试了一切但都失败了。我不知道我的类实例和继承结构有什么问题。我的第一语言是Java,我对C ++知之甚少。 我想通过编译器将所有文件连接到一个命名空间。这是我的代码中的损坏部分。希望有些人可以提供帮助,我工作几个月:

<FILE: ActivationFunction.h>

...
using namespace std;

namespace bl {

class ActivationFunction {  // This is used to be an abstract class


public:

ActivationFunction() {}
virtual double p(double);
virtual double derivate(double);



 };

class Exponential : public ActivationFunction { /* Concrete Implementation inherits from abstract class */

public:

Exponential(){} //seems to be an error ?

double p(double n) {

 return 1/(1 + exp(n));

}

double derivate(double n){
double r = 1/(1+exp(n));
return r - (r*r);
}
};

class Ident : public ActivationFunction{ /* Concrete Implementation inherits from abstract class */


public:

Ident(){}

double p(double n){
 return n;
 }

 double derivate(double n){
 return 1.0;
 }

 };

}

<FILE: Neuron.h>

#include...

namespace bl {/* all the files shall be fused together to one namespace BUT from different files */

class Neuron{ ////seems to be an error



private:
double currentActivity;
double derivated;
ActivationFunction phi;


public:
Neuron(ActivationFunction f_act){ ////////////////////////seems to be an error

phi = f_act;
currentActivity = 0;
derivated = 0;



Neuron(const Neuron & N){//How to write a correct copy constructor ?
    currentActivity = N.currentActivity;
    derivated = N.derivated;
    }

    Neuron & operator = (const Neuron & N){// How to write a correct assignment operator ?
    Neuron Nscr = N; // is this necessery ?
    if (&Nsrc == this)
    return *this;
    else{
    Neuron * buffer = this;
    this.this = &Nscr;
    delete buffer;
    }

    return *this;


    }

Neuron & operator = (const Neuron & nscr){//How to write a correct assignment operator ?
if(&nscr == this)//////not declared in this scope ERROR
return *this;

this = nscr;
return *this;

}

}

...
};
}


<FILE: Layer.h>

#include...

using namespace std;

namespace bl {

class weighttable{...};

class Layer { // This is an abstract Class with use of Polymorphism (don't know how to implement this in C++ correctly
public:
  virtual void connect(Layer,Layer); // shall be override
  virtual void connect(Layer) = 0; //ideally the class have to be abstract

  Layer(){}

  weighttable in_weight;
  vector<Neuron> Cell;  //Vector of Neuron Objects

  unsigned int getNr(void) const{
  return neuronNr;
  }

  protected:
  unsigned int neuronNr;
  virtual void update(vector<double>,double); /*Only derived class should use the protected functions */
  virtual void calcActivity(void);

};


class hiddenL : public Layer {

    public:

    Layer down;
    Layer top;

    hiddenL(unsigned int nr, ActivationFunction ph){
     neuronNr = nr;
     //in_weight = NULL; ?
     Neuron z; //seems to be an error
     for(int n = 0; n < neuronNr ; n++){// initializing with class variables
     z = Neuron(ph);
     Cell.push_back(z);
}
                                               }


void update(vector<double> delta, double nu) {
...

down.update(mdelta,nu);//////////////////error caused by Protected

                                                  }

void calcActivity(void) {
...

top.calcActivity(); ///////////////////////////error caused by protected

                         }

void connect (Layer sub, Layer up){

    down = sub;
    top = up;
    in_weight = weighttable(neuronNr, down.getNr() + 1, 1);

                                      }

void connect(Layer x){
assert(false);
}

};


class inputLayer : public Layer {

Layer top;
public:

inputLayer(){}

inputLayer(int nr, ActivationFunction basis) {


neuronNr = nr;
Neuron x; // use as double x; this is a class variable

for(int n = 0; n < neuronNr ; n++){
x = Neuron(basis);
Cell.push_back(x); }

                                              }

inputLayer(int nr){

//in_weight = nullptr; ?
neuronNr = nr;
ActivationFunction basis = Ident(); //seems to be an error
Neuron x; //seems to be an error

for(int n = 0; n < neuronNr ; n++){
x = Neuron(basis);

Cell.push_back(x);                }

                  }

void connect(Layer t){
top = t;
                     }

void netActivity(vector<double> data){
...

top.calcActivity();//error caused by protected
}

void update(vector<double> end, double n){
return;

}

void calcActivity(void){
return;
}

void connect(Layer x, Layer y){
top = x;
}

};

class OutL : public Layer {

public:

Layer down;

OutL(){}

OutL(int nr){

// is „in_weight“ = nullptr; correct ?
neuronNr = nr;
ActivationFunction o = Ident();

for(int n = 0; n < neuronNr ; n++){
Neuron y = Neuron(o);
Cell.push_back(y);

                                  }
}

void connect(Layer dlayer){

down = dlayer;

in_weight = weighttable(neuronNr, down.getNr() + 1, 1);

                          }

void bpupdate(vector<double> t, double nu){
...

down.update(d,nu);//protected

                                                   }
                                                                    }
void calcActivity(void) {
...
return;
                        }

void update(vector<double> no, double l){
return;
}

void connect(Layer x, Layer y){
down = x;
}

};

}

<FILE: net.h>

#include...

using namespace std;

   namespace bl  {

class net{

public:

OutL result;  //OutL class variable
inputLayer input; // inputLayer class variable
//////referece-problems

vector<Layer> level;  /* vector of Layer Objects, using polymorphism BUT referece wrong(not a type error) */
net(vector<int> M){

   int End = M.size() - 1; // last index of M
   Exponential act = Exponential(); /* creating an concrete Object of an ActivationFunction abstract class */
   Layer hidden; //a class type variable using polymorphism
   input = inputLayer(M[0]); ///////////////scope-error, no idea why//////
   level.push_back(input);   // POLYMORPHISM///////////////////type-error
   for(int l = 1; l < M.size() - 1 ; l++){
   hidden = hiddenL(M[l],act);
   level.push_back(hidden);//////////////////type-error
   }

   result = OutL(M[End]); ////////////////////////scope-error//////
   level.push_back(result);//POLYMORPHISM///////////////type-error

input.connect(level[1]); /////////type-error
result.connect(level[End-1]);///////////////error/////

for(int h = 1; h < level.size()-1; h++)
level[h].connect(level[h-1],level[h+1]); //type-error

}
...

};

}

<FILE: main.cpp>

#include <iostream>
#include <vector>
#include "ActivationFunction.h"
#include "Neuron.h"
#include "weighttable.h"
#include "Layer.h"
#include "net.h"


using namespace std;



int main(int argc, char** argv){


vector<int> form = {1,20,1}; 
bl::net knn = bl::net(form); 
knn.initialize_weights_random(-4.0,4.0);
vector<double> in = {4.3};
knn.test(in); 


return 0;

}; // is it right so ?

1 个答案:

答案 0 :(得分:1)

您发布的代码既不完整或有效,也不易读或不易理解。

看起来你正在编写某种神经元网络模拟程序。如果您对该语言完全陌生,那么这可能不是用C ++编写代码的最佳起始问题。也许你应该退一步,先学习一些关于语言的基础知识。

在线一些教程:

此外,如果您对命名空间布局和最佳做法指南感兴趣,请查看Google C ++样式指南:

要自动检查CPP代码的指南兼容性,请尝试使用cpplint:

只需几点指导即可开始。你的问题在这里是不正确的;请仔细阅读C ++基础知识后再重复一遍。