incomplete type

Hello, I have a lot of errors but of the same class I think

Chromosome.cc: In constructor ‘Chromosome::Chromosome(Individual**, int)’:
Chromosome.cc:12:29: error: invalid use of incomplete type ‘class Individual’
sequence = new Individual[s];
^
In file included from Chromosome.cc:6:
Chromosome.h:6:7: note: forward declaration of ‘class Individual’
class Individual;
^~~~~~~~~~
Chromosome.cc:15:13: error: invalid use of incomplete type ‘class Individual’
sequence[i]=indiv[i];
...............

THANKS!

Chromosome.cc looks like:

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
  #include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
#include <vector>
#include "Chromosome.h"
using namespace std;

Chromosome::Chromosome(Individual *indiv[], int s)
  : size(s)
{
	sequence = new Individual[size];
	for(int i;i<size;i++)
	{
		sequence[i]=indiv[i];
	}
}
Chromosome::Chromosome(int s)
  : size(s)
{
	vector<Individual> sequence;
	//sequence = new Sequence[s];
	vector<string> fact={"r","M","sin(teta)","1","2"};
	vector<string> operacion={"+","-","*","/","^"};
	unsigned int seed{0};
	srand(static_cast<unsigned int>(time(0)));
	int N =	rand()%CHROM_SIZE;
	int i = rand()%N;
	int j = rand()%N;
	for(int i=0;i<fact.size();i++)
	{	
		for(int j=0;j<operacion.size();j++)
		{
			sequence.push_back(Individual(fact[i], operacion[j],N));
		}
	}	  
}
Chromosome::~Chromosome()
{
  delete[] sequence;
}
int Chromosome::getSize(){ return size; }
Individual* getSequence()
{
	return sequence;
}
Last edited on
The compiler needs to know what an Individual is before it can make use of it. You have to show it all about an Individial. Typically, this is done by including a header file. Where is your declaration of Individual class?

A forward declaration is not enough. A forward declaration is just a promise that it does exist, but to actually make use of one, a forward declaration is not enough.
Also:
sequence = new Individual[size];

No no no. No.

Don't use new. Don't use delete. Don't use arrays. This is C++. We have vectors.
Also, your constructors make no sense. One of them thinks sequence is an array, one of them creates something called sequence that is a vector.

This constuctor,
1
2
Chromosome::Chromosome(int s)
  : size(s)

goes to a lot of effort to create and populate vector<Individual> sequence; and promptly throws it away at the end of the constructor. You're just throwing it away. This makes no sense.
Topic archived. No new replies allowed.