need help creating a class

So I've been using C++ for of months now, and have decided to finally start braving classes...

I am trying to create a class called 'braininfo' which contains some functions in regards to creating and destroying a neural network. The idea so far is that it creates a couple of vectors, and allows for me to also define how much space to reserve for each vector. I did this using a constructor.

The aim of the "allocate" function is to change the amount of space which I have allocated to the vectors, if I choose to do so at a later date. However, when I run the code I get the following errors:

1
2
3
4
5
error: 'class braininfo' has no member named 'synapse_in'
error: 'class braininfo' has no member named 'synapse_out'
error: 'class braininfo' has no member named 'synapse_weight'
error: 'class braininfo' has no member named 'synapse_state'
error: 'class braininfo' has no member named 'innov'


Here is the code that I am using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
#include <vector>

using namespace std;

class braininfo {
    public:
        braininfo(int length) {
            vector<int>   * synapse_in     = new vector<int>    (length); // source neuron
            vector<int>   * synapse_out    = new vector <int>   (length); // target neuron
            vector<float> * synapse_weight = new vector <float> (length); // synapse weight
            vector<bool>  * synapse_state  = new vector <bool>  (length); // boolean switch; 1 - synapse is enabled. 0 - synapse is disabled
            vector<int>   * innov          = new vector <int>   (length); // inovation number
        }

        void allocate(int length) {
            this->synapse_in.reserve(length);
            this->synapse_out.reserve(length);
            this->synapse_weight.reserve(length);
            this->synapse_state.reserve(length);
            this->innov.reserve(length);
        }

        ~braininfo() {
            delete this->synapse_in;
            delete this->synapse_out;
            delete this->synapse_weight;
            delete this->synapse_state;
            delete this->innov;
        }
};

int main()
{
    braininfo mybrain(100);
    return 0;
}


I would really appreciate some advice as to what it is that is causing the error, and whether or not I need those "this" statements...
Ok so I discovered an obvious mistake and now I have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class braininfo {
    public:
        vector<int>   * synapse_in;
        vector<int>   * synapse_out;
        vector<float> * synapse_weight;
        vector<bool>  * synapse_state;
        vector<int>   * innov;

        braininfo(int length) {
            synapse_in     = new vector <int>   (length); // source neuron
            synapse_out    = new vector <int>   (length); // target neuron
            synapse_weight = new vector <float> (length); // synapse weight
            synapse_state  = new vector <bool>  (length); // boolean switch; 1 - synapse is enabled. 0 - synapse is disabled
            innov          = new vector <int>   (length); // inovation number
        }

        void allocate(int length) {
            (*this->synapse_in).reserve(length);
            (*this->synapse_out).reserve(length);
            (*this->synapse_weight).reserve(length);
            (*this->synapse_state).reserve(length);
            (*this->innov).reserve(length);
        }

        ~braininfo() {
            delete this->synapse_in;
            delete this->synapse_out;
            delete this->synapse_weight;
            delete this->synapse_state;
            delete this->innov;
        }
};


But now the compiler is telling me this:

1
2
3
4
5
6
7
8
9
warning: 'class braininfo' has pointer data members
warning: but does not override braininfo(const braininfo&)'
warning: or 'operator=(const braininfo&)'

warning: 'braininfo::synapse_in' should be initiated in the member initialization list
warning: 'braininfo::synapse_out' should be initiated in the member initialization list
warning: 'braininfo::synapse_weight' should be initiated in the member initialization list
warning: 'braininfo::synapse_state' should be initiated in the member initialization list
warning: 'braininfo::innov' should be initiated in the member initialization list 


I don't really understand what the problem is with having pointer data members (or how I can avoid it in this particular case, as the vectors might be very large). Also, I have no idea what is mean by the member initialization list, at least not in this context. I thought that the constructor I was calling was initialising the class?
Your problem stems from the fact that you have declared your variables inside the constructor. These are not member variables, but local variables that will go out of scope when the constructor has finished executing.

You need to any member variables, as you have done your methods. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class myClass
{
    int *myInt;    //the member variables are declared inside the class and not 
    char *myChar;    //inside a method of that class

    //constructor
    myClass()
    {
        myInt = new int();    //member methods are initialised inside the constructor
        myChar = new char();
    }

    //and so on
};


Notice in the above example I have referred to the classes members directly by name, with no use of the this pointer. A class has full access to all of it's own members and methods (public, private and protected. Atleast I think so :) ).

But another class cannot invoke a classes private members and methods from outside of the class. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class myClass
{
    public:
    int notsosecret;

    private:
    int secret;
};

int main()
{
    myClass mclass;
    mclass.secret = 10;    //this will cause an error

    mclass.notsosecret = 10;    //no problem
}


I can't tell you too much about the this pointer as I've never really used it but I'm sure a more experienced user can help you out with that.

You probably have already seen the tutorials on this website on classes, but in case you haven't here are some links:

Classes (part 1): http://www.cplusplus.com/doc/tutorial/classes/
Classes (part 2): http://www.cplusplus.com/doc/tutorial/classes2/

Classes (part 2) has a section on the this pointer.
Topic archived. No new replies allowed.