little error

Hello, it must be a nonsense but i can not see it
I hace two files Proincipal.cpp with the main and other Individuo which code is:

#include <iostream>
#include <vector>
#include <string>

using namespace std;
class Individuo{
public:
Individuo(string str1, string str2): factor{str1}, operacion{str2}
{ cout << "Initialised with " << factor[0] << " and " << operacion[0] << '\n'; }

string getFactor(int j)
{
return factor[j];
}
void setFactor(string ftr)
{
factor.push_back(ftr);
}
string getOperacion(int i)
{
return operacion[i];
}
void setOperacion(string op)
{
operacion.push_back(op);
cout<<"En metodo set operacion:"<<op<<endl;
}
void setFormula(int j, int i)
{
formula = getFactor(j) + getOperacion(i) + formulaN;
cout<<"formula...en Individuo.= "<<formula<<" j="<<j<<" i="<<i<<endl;
cout<<"Individuo...getFactor(j)="<<getFactor(j)<<endl;
cout<<"Individuo...getOperacion(i)="<<getOperacion(i)<<" "<<operacion[i]<<endl;
formula=formulaN;
}
void setFormulaN()
{
formulaN=formula;
}

private:

string formulaN="",formula="";
vector<string> factor;
vector<string> operacion;
};




The problem is that getOperacion don't return anything on the contrary that getFactor...but they are symetric...they work the same
what i'm doing wrong?
thanks
You’ve opened five threads which, in my opinion, are all but clear.
Perhaps you should start from simpler codes?

When you post, please add compilable or nearly compilable code, not just a rough draft or a bunch of lines.
And, yes, put "code" around your code. This way:
1
2
3
4
5
[ code ] <-- but without spaces

your code (well indented!)

[ /code ] <-- but without spaces


Maybe I’ll be scolded for this, but... here are some hints:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>


class Individuo {
public:
    Individuo(std::string factor_arg, std::string operacion_arg);

    std::string getFactor(int j) const;
    void setFactor(const std::string& ftr);
    std::string getOperacion(int i) const;
    void setOperacion(const std::string& op);
    void setFormula(int j, int i);
    void setFormulaN();
    int getFactorsNumber() const;
    int getOperacionNumber() const;

private:
    std::string formulaN;
    std::string formula;
    std::vector<std::string> factor;
    std::vector<std::string> operacion;
}; // end class Individuo


Individuo::Individuo(std::string factor_arg, std::string operacion_arg)
    : factor { factor_arg }
    , operacion { operacion_arg }
{
    std::cout << "Initialised with " << factor.at(0)
              << " and " << operacion.at(0) << '\n';
}


std::string Individuo::getFactor(int j) const
{
    return factor.at(j);
}


void Individuo::setFactor(const std::string& ftr)
{
    factor.push_back(ftr);
}


std::string Individuo::getOperacion(int i) const
{
    return operacion.at(i);
}


void Individuo::setOperacion(const std::string& op)
{
    operacion.push_back(op);
    std::cout << "En metodo set operacion: " << op << '\n';
}


void Individuo::setFormula(int j, int i)
{
    formula = getFactor(j) + getOperacion(i) + formulaN;
    std::cout << "formula(" << j << ", " << i << "):  "
              << "factor: " << std::setw(16) << std::left << factor.at(j)
              << "operacion: " << operacion.at(i)
              << '\n';
    formula = formulaN;
}


void Individuo::setFormulaN()
{
    formulaN = formula;
}


int Individuo::getFactorsNumber() const
{
    return static_cast<int>(factor.size());
}


int Individuo::getOperacionNumber() const
{
    return static_cast<int>(operacion.size());
}


int main()
{
    Individuo ind("initial_factor", "initial_operation");

    ind.setFactor("r");
    ind.setFactor("M");
    ind.setFactor("teta");
    ind.setFactor("1");
    ind.setFactor("2");

    ind.setOperacion("+");
    ind.setOperacion("-");
    ind.setOperacion("*");
    ind.setOperacion("/");
    ind.setOperacion("^");
    ind.setOperacion("sin");

    for (int j = 0, loop_n { 1 }; j < ind.getFactorsNumber(); ++j) {
        for (int i = 0; i < ind.getOperacionNumber(); ++i, ++loop_n) {
            std::cout << std::setw(2) << std::right << loop_n << ") ";
            ind.setFormula(j, i);
        }
    }
}


Output:
En metodo set operacion: +
En metodo set operacion: -
En metodo set operacion: *
En metodo set operacion: /
En metodo set operacion: ^
En metodo set operacion: sin
 1) formula(0, 0):  factor: initial_factor  operacion: initial_operation
 2) formula(0, 1):  factor: initial_factor  operacion: +
 3) formula(0, 2):  factor: initial_factor  operacion: -
 4) formula(0, 3):  factor: initial_factor  operacion: *
 5) formula(0, 4):  factor: initial_factor  operacion: /
 6) formula(0, 5):  factor: initial_factor  operacion: ^
 7) formula(0, 6):  factor: initial_factor  operacion: sin
 8) formula(1, 0):  factor: r               operacion: initial_operation
 9) formula(1, 1):  factor: r               operacion: +
10) formula(1, 2):  factor: r               operacion: -
11) formula(1, 3):  factor: r               operacion: *
12) formula(1, 4):  factor: r               operacion: /
13) formula(1, 5):  factor: r               operacion: ^
14) formula(1, 6):  factor: r               operacion: sin
15) formula(2, 0):  factor: M               operacion: initial_operation
16) formula(2, 1):  factor: M               operacion: +
17) formula(2, 2):  factor: M               operacion: -
18) formula(2, 3):  factor: M               operacion: *
19) formula(2, 4):  factor: M               operacion: /
20) formula(2, 5):  factor: M               operacion: ^
21) formula(2, 6):  factor: M               operacion: sin
22) formula(3, 0):  factor: teta            operacion: initial_operation
23) formula(3, 1):  factor: teta            operacion: +
24) formula(3, 2):  factor: teta            operacion: -
25) formula(3, 3):  factor: teta            operacion: *
26) formula(3, 4):  factor: teta            operacion: /
27) formula(3, 5):  factor: teta            operacion: ^
28) formula(3, 6):  factor: teta            operacion: sin
29) formula(4, 0):  factor: 1               operacion: initial_operation
30) formula(4, 1):  factor: 1               operacion: +
31) formula(4, 2):  factor: 1               operacion: -
32) formula(4, 3):  factor: 1               operacion: *
33) formula(4, 4):  factor: 1               operacion: /
34) formula(4, 5):  factor: 1               operacion: ^
35) formula(4, 6):  factor: 1               operacion: sin
36) formula(5, 0):  factor: 2               operacion: initial_operation
37) formula(5, 1):  factor: 2               operacion: +
38) formula(5, 2):  factor: 2               operacion: -
39) formula(5, 3):  factor: 2               operacion: *
40) formula(5, 4):  factor: 2               operacion: /
41) formula(5, 5):  factor: 2               operacion: ^
42) formula(5, 6):  factor: 2               operacion: sin


I wasn't sure about what you really wanted to do...
Topic archived. No new replies allowed.