no match for ‘operator=’

Hello, maybe somebody can help me, I have these errors:

Individual.cc: In member function ‘void Individual::setGene(int, Rama)’:
Individual.cc:39:33: error: no match for ‘operator=’ (operand types are ‘std::vector<Rama>’ and ‘Rama’)
chromosome->sequence[offset] = gene;
^~~~
In file included from /usr/include/c++/8/vector:69,
from Individual.cc:5:
/usr/include/c++/8/bits/vector.tcc:186:5: note: candidate: ‘std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = Rama; _Alloc = std::allocator<Rama>]’
vector<_Tp, _Alloc>::

.....
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
  #include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
#include <vector>
#include "Individual.h"
#include "Chromosome.h"
//#include "Rama.h"

using namespace std;

Individual::Individual(vector<Chromosome> chrom,int size) // Cuts off digits past size
{
	vector<Chromosome> chromosome;
	for(int i=0;i<size;i++)
	{
		chromosome.push_back(chrom[i]);
	}
//	chromosome = new Chromosome(chrom,size);
}
Individual::Individual(int size)
{
//  chromosome = new Chromosome(size);
	vector<Chromosome> chromosome;
	for(int i=0;i<size;i++)
	{
		chromosome.push_back(Chromosome(size));
	}
}

Individual::~Individual()
{
  delete chromosome;
}

Chromosome* Individual::getChromosome(){ return chromosome; }
int Individual::getChromosomeLength(){ return chromosome->getSize(); }
void Individual::setGene(int offset, Rama* gene){ 
	chromosome->sequence[offset] = *gene;
}
Rama 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();
  }
}




Chromosome.h is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CHROMOSOME_H
#define CHROMOSOME_H
#include <vector>
#include "defs.h"
//#include "Individual.h"

class Individual;
class Chromosome {
friend class Individual;
  public:
    Chromosome(vector<Rama>*, int);
      /* Generates chromosome from input array     */
    Chromosome(int);
      /* Generates  sequence of defined size */
    ~Chromosome();
    int getSize();
    vector<Rama>* getSequence();
  private:
    vector<Rama>* sequence;
    //Rama* sequence;
    int size;
};
#endif 
Last edited on
chromosome->sequence[offset] = gene;

One of these things is a vector. One of them is not. So this makes no sense. What does it mean? What are you trying to do?
I remove the * and I have the same problem!

besides Rama.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
#include<iostream>
#include<vector>
#include<string>

using namespace std;
Rama(string f, string o)
: factor(f), operacion(o){}
void Rama::setFactor(string factor)
{
	this->factor=factor;
}
void Rama::setOperacion(string operacion)
{
	this->operacion=operacion;
}
string Rama::getFactor()
{
	return factor;
}
string Rama::getOperacion()
{
	return operacion;
}
string Rama::toString()
{
	return factor + operacion;
}
string Rama::getFormula(int n)
{	
	if (n<=1)
	{	
		return this->getFactor();
	}
	else
	{
		return "("+this->getFactor()+this->getOperacion() + getFormula(n-1)+")";
	}
}

chromosome->sequence[offset] = *gene


chromosome->sequence is a vector of objects Rama
chromosome->sequence[offset] is an object of Rama
and *gene is an object of Rama
so I think the equivalence is correct

Can you explain please
My project is to build a software that search a solution to a differential equation based in genetic algorithms, I hope somebody is interested in this project
chromosome->sequence is a vector of objects Rama

No.

vector<Rama>* sequence;
sequence is a pointer to a vector

So chromosome->sequence[offset] is a non-existent vector, in an array of vector that also doesn't exist, because that's what you're trying to reach when you use [] on a pointer; put short,

chromosome->sequence[offset] is a vector of objects of Rama, that doesn't even exist.
Last edited on
I see that I must steel persevere in my c++
Besides I put the inicial code in GitHub, if you are interested
https://github.com/psosmol/GAAnalyticalSol/

Thanks
Topic archived. No new replies allowed.