allocator whitout define

Hello, I have 2 files: Individuo.h and Principal.cpp

Principal.cpp code:
#include <iostream>
#include "Individuo.h"

using namespace std;

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;
}

private:
vector<string> factor;
vector<string> operacion;
};

I have problems in allocator whitout define or so
what can I do?
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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'; }
private:
   vector<string> factor;
   vector<string> operacion;
};

int main(){
   Individuo ind("1","0");
   return 0;
}
Last edited on
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:
1
2
3
4
  Individuo(string str1, string str2)
  : factor( 1, str1 ), operacion( 1, str2 )
  {
  }

I have the same problem despite the changes ...what can I do?
Tanks
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?
Last edited on
I don't kinow anything about cpp.sh I use ubuntu and g++ ... is ti fine?
Did my code sample run on your compiler?
yes, it works...but I need two files, one Individuo with the definition of the class and the other with the main
hey, know I get aware that compile with g++ but no with gcc, is it normal?
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.
Last edited on
thanks for all
Registered users can post here. Sign in or register to post.