I am trying to create a double pointer. never done it before. Problem is currently in intializepopulation();
Error C2106 left of operand must be l-value.
EA.h
#ifndef EA_H_INCLUDED
#define EA_H_INCLUDED
#include <string>
#include "phrase.h"
#include <iostream>
class EA:public phrase
{
public:
EA();
~EA();
void setgenerationnumber(int generationnumber);
int getgenerationnumber();
void setpopulationsize(int populationsize);
void initalizepopulation();
void mutate();
//void crossover();
std::string bestofpopulation();
private:
int populationsize_;
int generationnumber_;
char** population_;
};
#endif // INTERFACE_H_INCLUDED
EA.cpp
#include "EA.h"
EA::EA()
{
populationsize_=NULL;
generationnumber_=0;
population_=NULL;
}
EA::~EA()
{
delete population_;
}
void EA::setgenerationnumber(int generationnumber)
{
generationnumber_=generationnumber;
}
int EA::getgenerationnumber()
{
return generationnumber_;
}
void EA::setpopulationsize(int populationsize)
{
populationsize_=populationsize;
}
void EA::initalizepopulation()
{
for(int j=0; j<populationsize_; j++)
{
population_=new char*[j];
for(int k=0; k<getphraselength(); k++)
{
(*(population_)+j)=new char[k];
population_[j][k]=(char)generatenewIndividual();
std::cout<<(*population_);
}
std::cout<<"\n";
}
}
/*void mutate()
void crossover()
std::string bestofpopulation()
*/
phrase.h
#ifndef PHRASE_H_INCLUDED
#define PHRASE_H_INCLUDED
#include <string>
class phrase
{
public:
phrase();
void setphraselength(std::string phraselength);
int getphraselength();
//randomly intialize a string
char* generatenewIndividual();
char randomizecharacter();
private:
int phraselength_;
};
#endif // INTERFACE_H_INCLUDED
phrase.cpp
#include "phrase.h"
#include <string>
phrase::phrase()
{
phraselength_=0;
}
void phrase::setphraselength(std::string phraselength)
{
phraselength_=phraselength.length();
}
int phrase::getphraselength()
{
return phraselength_;
}
char phrase::randomizecharacter()
{
int n = rand() % 26;
char c = (char)(n+'a');
return c;
}
char* phrase::generatenewIndividual()
{
char* random = new char[phraselength_];
for(int i=0; i<=phraselength_; ++i)
{
random[i]=randomizecharacter();
}
random[phraselength_]=NULL;
return random;
}
main
#include <iostream>
#include <string>
#include "phrase.h"
#include "EA.h"
#include <cstdlib>
#include <time.h>
int main()
{
phrase a;
EA b;
std::string phrase;
int populationsize;
//int percentmutation;
std::cout<<"enter a population size\n";
std::cin>>populationsize;
std::cin.ignore();
b.setpopulationsize(populationsize);
//std::cout<<"enter the percentage you would like to mutate\n";
//std::cin>>percentmutation;
std::cout<<"enter a string\n";
getline(std::cin,phrase);
a.setphraselength(phrase);
//std::cout<<a.getphraselength();
srand ((unsigned)time(0));
b.initalizepopulation();
/*while(phrase!=bestofpopulation())
{
//mutate and crossover to create new population
}*/
return 0;
}
Thanks