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

IT doesn't enter second loop don't know why. Any suggestion?

void EA::initalizepopulation()
{
	
	
	for(int j=0; j<populationsize_; j++)
	{
                std::cout<<"enter first loop\n";
		population_=new char*[j];
		for(int k=0; k<getphraselength(); k++)
		{
                        std::cout<<"enter second loop\n";
			(*((population_)+j))=new char[k];
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<(*population_);
		}
		std::cout<<"\n";
	}
	
}

IT doesn't enter second loop don't know why. Any suggestion?

Yep.
Split for(int k=0; k<getphraselength(); k++) into 2 statements. What call the function every time you go through the loop? Make it:

len = getphraselength();
for(int k=0; k<len; k++)

Now you can display len and see if the value lets the loop run.

In general, don't call functions in the parameters of the for if you can help it.

why is saying length is 0 when it isn't in my main???

void EA::initalizepopulation()
{
	int len;
	for(int j=0; j<populationsize_; j++)
	{
                std::cout<<"enter first loop\n";
		population_=new char*[j];
		len = getphraselength();
		for(int k=0; k<len; k++)
		{
            std::cout<<"enter second loop\n";
			(*((population_)+k))=new char[k];
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<(*population_);
		}
		std::cout<<"\n";
	}
 
}

somehow in the main i get the correct length to appear but in doesn't when i call in in intializepopulation. Perhaps i should have EA constructor to which i call phrase instead of using inheritance.

most updated version:

for(int j=0; j<populationsize_; j++)
	{
		std::cout<<j<<std::endl;
                std::cout<<"enter first loop\n";
		population_=new char*[populationsize_];
		std::cout<<"length is"<<phraselength;
		for(int k=0; k<phraselength; k++)
		{
                        std::cout<<"enter second loop\n";
			(*(population_))=new char[phraselength];
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<(*population_);
		}
		std::cout<<"\n";
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.