Problem when initializing a vector within a class

1

Good morning, I have a problem when starting a vector. This starts when I call the Species function of the class. In principle it is created well, but when I try to use it, it tells me that it has size 0.

This is the class:

#include "Especie.hh"

Especie::Especie() {
    cin >> N;
    cin >> l0;
    int m = N + 2;
    vector<int> ln (m);
    for(int i = 2; i < m; ++i) cin >> ln[i];
    cin >> ln[0] >> ln[1];
    cout << ln.size() << endl; //Aquí vemos que el tamaño es el correcto (7 con la entrada concreta que he puesto)
}

int Especie::n_normals() {
    return N;
}


int Especie::tall_sexuals() {
    return l0;
}


int Especie::mida(int i) {
    return ln[i];
}

int Especie::mida2() {
    return ln.size();
}

And this is the test code:

#include "Especie.hh"

int main() {
    Especie e;
    cout << e.mida2() << endl;  //Aquí ya se ve que tiene longitud 0
    int n = e.n_normals();
    cout << n << endl;
    int ts = e.tall_sexuals();
    cout << ts << endl;

    int m;
    for(int i = 0; i < n+2; ++i) {
        m = e.mida(i);
        cout << m;
    }
    cout << endl;
}

Here is a test entry: 5 3 6 7 6 7 6 5 3

I also put the hh:

#ifndef ESPECIE_HH
#define ESPECIE_HH

#include <vector>
#include <iostream>
using namespace std;

class Especie {

private:
    int N; //num. cromosomas normales
    int l0; //punto de corte
    vector<int> ln; //vector con la long. de los cromosomas

public:

    Especie(); //lee los datos de la especie en el canal de entrada

    int n_normals(); //Devuelve el num. de cromosomas normales N

    int tall_sexuals(); //Devuelve el punto de corte en los crom. sexuales

    int mida(int i); //Devuelve ln[i](long. del crom. i)

    int mida2(); //Devuelve el tamaño de ln
};
#endif

There are things in Catalan but I guess it's not a big problem.

I hope you can help me

    
asked by Kuznov 15.05.2017 в 18:05
source

1 answer

2

Bona tarda! ;)

Problem.

You have a problem when mixing local variables with member variables, specifically in the class constructor, I frame it with comments:

Especie::Especie() {
    cin >> N;
    cin >> l0;
    int m = N + 2;

    /* Este 'ln' es local a la funcion 'Especie::Especie',
       no hace referencia a la variable miembro 'Especie::ln' */
    vector<int> ln (m);
  //~~~~~~~~~~~~~~~~~~~ <-------- VARIABLE LOCAL A Especie::Especie.

    for(int i = 2; i < m; ++i) cin >> ln[i];
    cin >> ln[0] >> ln[1];
    cout << ln.size() << endl;
}

Solution.

You must not build a new variable, use the existing one (as you already do with N and l0 ), I frame it with comments:

Especie::Especie() {
    cin >> N;
    cin >> l0;
    int m = N + 2;

    /* Este 'ln' es local a la funcion 'Especie::Especie',
       no hace referencia a la variable miembro 'Especie::ln' */
    ln.resize(m);
  //~~ <-------- VARIABLE MIEMBRO Especie::ln.

    for(int i = 2; i < m; ++i) cin >> ln[i];
    cin >> ln[0] >> ln[1];
    cout << ln.size() << endl;
}
    
answered by 15.05.2017 / 21:27
source