int main(){
//El primer gen es un factor y además "no operación"
Individuo ind("1","0");
return 0;
}
Individuo.h code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Individuo{
public:
Individuo(string str1, string str2){
factor[0]=str1;
operacion[0]=str2;
}
I'm not sure what error you do refer to, but I do see a clear error:
1 2 3 4 5 6 7 8 9 10 11 12
class Individuo{
public:
Individuo(string str1, string str2)
{
factor[0] = str1; // out of range error
operacion[0] = str2; // out of range error
}
private:
vector<string> factor;
vector<string> operacion;
};
The vectors are empty. They do not have element [0] yet.
You could push_back, emplace_back, or preferably initialize:
With vectors initialised to have non-zero size as per mine and/or @Keskiverto's suggestion the code runs fine in cpp.sh (the online compiler - gear-wheel icon to the top-right of a runnable code sample). Does it do the same with your compiler?
yes, it works...but I need two files, one Individuo with the definition of the class and the other with the main
Well, you can put your class definition back in a separate file. That wasn't your problem. It was the lack of size and/or initialisation that was the problem. Note the initialisation of the vectors in the constructor code:
1 2
Individuo(string str1, string str2) : factor{str1}, operacion{str2}
{ /* Anything else in the constructor */ }
Without this, or some other approach to give a non-zero size to your vectors, then factor[0] and operacion[0] would not exist.
I only put the contents together so that it would run on this site's online compiler (cpp.sh). Then people don't have to download multiple files in order to test it - they can just run it online.
hey, know I get aware that compile with g++ but no with gcc, is it normal?
I imagine that g++ will automatically call gcc with the appropriate flags and libraries set for C++, rather than C, code.