简体   繁体   中英

How to have a refernce type in a struct c++

So I am trying to make a neural network in c++ to help me learn the language, I have come across an issue in which inside a struct I would like to have a reference to an object.

In my neuron class, I would like to have a vector of the struct Connection, this connection struct will store a neuron and a weight. The issue is when I try to run the code I get a variety of errors.

This is the simplified code

Neuron.h

#pragma once
#include <vector>
using namespace std;

struct Connection{
    Neuron& neuron;  // unexpected token preceding ';' // syntax error: missing ';' before '&' // missing type specifier - int assumed
    double weight;
}

class Neuron
{
public:
    Neuron();
    void addPrevLayer(vector<Neuron> &input_layer);
private:
    Layer temp_layer = Layer(); // will be overwritten, only way i could get around initialisation error
    Layer& previousLayer = temp_layer;
    vector<Connection> connections;

Neuron.cpp

#include "Neuron.h"

Neuron::Neuron(){}

void Neuron::addPrevLayer(vector<Neuron> &input_layer) {
    previousLayer = input_layer;
    for (int i = 0; i < input_layer.size(); i++){
        Connection temp_connection { previousLayer[i], (double)rand() / RAND_MAX }; // 'initializing': cannot convert from '_Ty' to 'double'
        connections.push_back(temp_connection);
    }
}

And for simplicity, the addPrevLayer function is where I am using the struct.

So issue #1: I know the previousLayer variable isn't necessary but I am intrigued as to how to get it to work anyway. I don't like how I have to initialize previousLayer with tempLayer, is there a way in which I can initialize this in the constructor or when definining the variable. When I don't initialize it in the Neuron.h file I get an error stating - "Neuron::Neuron()" provides no initializer for: reference member "Neuron::previousLayer"

issue #2: I have written in the comments where I am getting the errors and what they say, figured that would be easier than describing where they are. The issue seems to be with defining a type to be a reference.

You need to forward-declare the class:

class Neuron;

struct Connection {
    Neuron& neuron;  
    double weight;
}

Do note that you need to initialize this reference in constructor of connection/during creation:

Neuron n;
Connection conn{ n, 3.14 };

Having references in classes is very annoying. Makes it difficult to copy the class etc. A more common thing is to put a pointer in the struct:

class Neuron;

struct Connection {
    Neuron* neuron;  
    double weight;
}

Whenever you write something like this:

struct Connection {
    Neuron neuron; // not a reference nor a pointer  
    double weight;
};

the compiler needs to know already what Neuron is, because neuron is a "true" object of that class, blood, bones, and skin; so it needs a definition of it, eg

struct Neuron { /* stuff */ };
struct Connection {
    Neuron neuron;  
    double weight;
};

In your use case, however,

struct Connection {
    Neuron& neuron;  
    double weight;
};

the compiler doesn't really need to know what Neuron is, because neuron is just a reference to such an object, which you can think of as pointer in disguise (eg Neuron* neuron; instead of Neuron& neuron; ), therefore the compiler only needs to know that Neuron is type, and not what it actually looks like, so a forward declaration of the class is enough:

struct Neuron;
struct Connection {
    Neuron& neuron;  
    double weight;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM