invalid user-defined
May 7, 2019 at 1:23pm UTC
Hello, I have a couple of errors and I can not find the cause, I hope somebody can help me!
Thanks
Individual.cc: In member function ‘void Individual::setGene(int, Individual*)’:
Individual.cc:28:33: error: invalid user-defined conversion from ‘Individual*’ to ‘const Individual&’ [-fpermissive]
chromosome->sequence[offset] = gene;
^~~~
Individual.cc:15:1: note: candidate is: ‘Individual::Individual(int)’ <near match>
Individual::Individual(int size)
^~~~~~~~~~
Individual.cc:15:1: note: conversion of argument 1 would be ill-formed:
Individual.cc is:
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
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
#include "Individual.h"
#include "Chromosome.h"
using namespace std;
Individual::Individual(string f, string o,int n) // Cuts off digits past size
:factor(f),operacion(o)
{
chromosome = new Chromosome(n);
}
Individual::Individual(int size)
{
chromosome = new Chromosome(size);
}
Individual::~Individual()
{
delete chromosome;
}
Chromosome* Individual::getChromosome(){ return chromosome; }
int Individual::getChromosomeLength(){ return chromosome->getSize(); }
void Individual::setGene(int offset, Individual* gene){
chromosome->sequence[offset] = gene;
}
Individual Individual::getGene(int offset){ return chromosome->sequence[offset]; }
void Individual::setFitness(double fit){ fitness = fit; }
double Individual::getFitness(){ return fitness; }
void Individual::printChromosome()
{
int size;
int i;
size = chromosome->getSize();
for (i = 0; i < size; i++){
cout << getGene(i).toString();
}
void Individual::setFactor(string factor)
{
this ->factor=factor;
}
void Individual::setOperacion(string operacion)
{
this ->operacion=operacion;
}
string Individual::getFactor()
{
return factor;
}
string Individual::getOperacion()
{
return operacion;
}
string Individual::toString()
{
return factor + operacion;
}
string Individual::getFormula(int n)
{
// int i = rand()%indiv.size();
// int j = rand()%indiv.size();
if (n<=1)
{
//return indiv[j].getFactor();
return this ->getFactor();
}
else
{
return "(" +this ->getFactor()+this ->getOperacion() + getFormula(n-1)+")" ;
}
}
Last edited on May 7, 2019 at 1:35pm UTC
May 7, 2019 at 1:31pm UTC
what are you calling setgene with?
May 7, 2019 at 2:15pm UTC
is the problem that the function setGene use an object of its own class?
May 7, 2019 at 2:36pm UTC
The problem is that chromosome->sequence[offset]
does not expect a pointer to Individual
. So what is sequence
?
May 7, 2019 at 3:01pm UTC
Chromosome.h is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#ifndef CHROMOSOME_H
#define CHROMOSOME_H
#include "defs.h"
//#include "Individual.h"
class Individual;
class Chromosome {
friend class Individual;
public :
Chromosome(Individual[], int );
/* Generates chromosome from input array */
Chromosome(int );
/* Generates random sequence of defined size */
~Chromosome();
int getSize();
Individual* getSequence();
private :
Individual* sequence;
int size;
};
#endif
May 7, 2019 at 3:09pm UTC
It's surely confusing that Chromosome
has an array of Individual
and Individual
an array of Chromosome
. It doesn't look right.
To solve your problem the assignment looks like this:
chromosome->sequence[offset] = *gene; // In this case '*' is the derefernce operator
The array holds Individual
not pointer to Individual
.
Topic archived. No new replies allowed.