typeDef不在类之间传递(喜欢)

时间:2017-11-09 20:01:48

标签: c++ syntax typedef

是的,所以我在C ++中构建一个神经网络作为一个起始项目,然而我遇到了一个(可能)简单的问题,类之间的类型定义。如果重要的话,我在视觉工作室2015工作。

因此,当我构建项目时,我收到以下错误

- >语法错误:标识符'layer'(第26行 - Neuron.h)

- >语法错误:标识符'layer'(第26行 - Neuron.h)[是的它列出了两次]

- > 'Neuron :: feedForward'函数不带1个参数(第37行Net.cpp)

- >查找预编译头时意外结束文件。你忘了在你的来源添加'#include stdafx.h吗? (第37行neuron.cpp)[最重要的不可能......]

当我在Neuron.h中突出显示visual studio中的类型值时,我得到以下定义:

typedef std :: vector<<'error-type'>>层

现在很明显,正如您将在下面的代码中看到的那样,Neuron类没有正确使用网络类中的typedef(包含在彼此中作为包含)。但是我不知道如何解决这个问题。

以下是代码:

net.h

#pragma once

#include "stdafx.h"
#include <iostream>
#include <assert.h>
#include <vector>
#include "Neuron.h"
//#include "TypeDefNeuralNet.h"


typedef std::vector<Neuron> Layer;
class Net {


public: 



    Net(const std::vector<unsigned> &topology);
    void feedForward(const std::vector<double> &inputVals);
    void backProp(const std::vector<double> &targetVales);
    void getResults(std::vector<double> &inputVals) const;
private:
    std::vector<Layer> m_layers; //m_layers[layerNum][neuronNum]
    double m_error;
    double m_recentAverageError;
    double m_recentAverageSoothingFacror;


};

Net.cpp

#include "stdafx.h"
#include "Net.h"


/*
class constructors and methods for Net.cpp/.h
*/
Net::Net(const std::vector<unsigned> &topology) {
    unsigned numLayers = topology.size();

    for (unsigned layerNum = 0; layerNum < numLayers; layerNum++) {
        m_layers.push_back(Layer());
        unsigned numOutputs = layerNum == topology.size() - 1 ? 0 : topology[layerNum + 1]; //inline or num outputs == 0 if last index or (:) topology at next index


        for (unsigned neuronNum = 0; neuronNum <= topology[layerNum]; neuronNum++) { //add bias neuron with <=
            m_layers.back().push_back(Neuron(numOutputs, neuronNum));

            std::cout << "made a neuron" << std::endl;
        }
    }

}

void Net::feedForward(const std::vector<double> &inputVals) {
    assert(inputVals.size() == m_layers[0].size());
    //set input
    for (unsigned k = 0; k < inputVals.size(); k++) {
        m_layers[0][k].setOutputVal(inputVals[k]); 
    }

    //feedforward
    for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++)
    {
        Layer &prevLayer = m_layers[layerNum - 1]; //get previous layer
        for (unsigned neuron = 0; neuron < m_layers[layerNum].size()-1; neuron++) {
            m_layers[layerNum][neuron].feedForward(prevLayer);
        }
    }
}


void Net::backProp(const std::vector<double> &inputVals) {

}

void Net::getResults(std::vector<double> &inputVals) const {

}

Neuron.h

#pragma once

#include "stdafx.h"
#include <vector>
#include <cstdlib>
#include <cmath>
#include "Net.h"


struct Connections {
    double weight;
    double deltaWeight;
};


class Neuron {
public:
    //constructor
    Neuron(unsigned numOutputs, unsigned myIndex);

    //getters setters
    void setOutputVal(double val) { m_outputVal = val;  };
    double getOutputVal() { return m_outputVal; };

    //function
    void feedForward(Layer &prevousLayer); 

private:
    double m_outputVal;
    unsigned m_myIndex;
    std::vector<Connections> m_outputWeights;
    static double randomWeight();
    static double transferFunction(double x);
    static double transferFunctionDervivative(double x);

};

Neuron.cpp

#include "Neuron.h"

Neuron::Neuron(unsigned numOutputs, unsigned myIndex) {

    for (unsigned k; k < numOutputs; k++)
    {
        m_outputWeights.push_back(Connections()); 
        m_outputWeights[k].weight = randomWeight(); 
    }
    m_myIndex = myIndex;
}

double Neuron::randomWeight() {
    return rand() / double(RAND_MAX);
}


void Neuron::feedForward(Layer &prevLayer) {

    double sum = 0.0; 
    for (unsigned pn = 0; pn < prevLayer.size(); pn++) {
        sum += prevLayer[pn].getOutputVal() *
            prevLayer[pn].m_outputWeights[m_myIndex].weight;
    }
    m_outputVal = Neuron::transferFunction(sum);
} 



double Neuron::transferFunction(double x) {
    return tanh(x); 
}

double Neuron::transferFunctionDervivative(double x){
    return 1 - x*x;
}

NeuroNetTestProject.cpp(切入点)可能没有问题,但包括在内。

#include "stdafx.h"
#include "Net.h"
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    //e.g. 


    vector<unsigned> topology;
    topology.push_back(3);
    topology.push_back(2);
    topology.push_back(1);

    Net myNet(topology);

    std::vector<double> inputVals; 
    std::vector<double> targetVals;
    std::vector<double> resultVals;


    myNet.feedForward(inputVals);
    myNet.backProp(targetVals);
    myNet.getResults(resultVals);

    system("pause");

    return 0;
}

无论如何我的赌注错误就是没有传递的图层标识符,我该如何解决?并在将来避免这种情况?

1 个答案:

答案 0 :(得分:0)

我认为你可以直接在net.h中声明神经元而不是#include“Neuron.h”,如下所示:

#include <vector>
class neuron;
typedef std::vector<neuron> layer;

class net {
    std::vector<layer> layer_;
public:

};

class neuron {
    void feed_forward(layer&);
};

但我不明白为什么Some Programmer Dude的评论不适合你,因为这应该足够了。