does not name a type

Hello I have an error message among others

Chromosome.h:16:5: error: ‘Individual’ does not name a type
Individual* sequence;

thanks

the code for Chromosome.h is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CHROMOSOME_H
#define CHROMOSOME_H
#include "defs.h"
#include "Individual.h"
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();
    int* getSequence();
  private:
    Individual* sequence;
    int size;
};
#endif 


the code for 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
76
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace std;

#include "Individual.h"
#include "Chromosome.h"

Individual::Individual(string f, string o) // Cuts off digits past size
:factor(f),operacion(o){}
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(vector<Rama>* ram, int n)
{	
	int i = rand()%ram.size();
	int j = rand()%ram.size();
	vectorPosicion.push_back(i);
	if (n<=1)
	{	
		return ram[j].getFactor();
	}
	else
	{
//		cout <<"en return n-1:"<<n<<endl;
		return "("+ram[i].getFactor()+ram[i].getOperacion() + setFormula(ram,n-1)+")";
	}
}
private:
	string factor;
	string operacion;
}
Last edited on
from what you gave us, your include order and all look correct.
so I am going to ask about 'other errors' in your comment. If you have errors in the individual class itself, then it won't register as a type, and youll get this further down. Which goes back to the 'resolve the top error and hit go again' for small projects (clearly need to look more when you get to stuff that takes more than a min or 2 to compile).

if all else fails try to compile a stub project with just the individual class and one instance of it in main.
Last edited on
It's always hard for to follow this kind of error without having the code in front of me, but I think there actually is a possible dependency issue here.

There error is happening in Chromosome.h... based on the code you're giving, let's assume it's not compiling individual.cc correctly.

When we first go to compile individual.cc, first we #include Individual.h.
But... I can see from this line
Chromosome* Individual::getChromosome(){ return chromosome; }
That you need Individual to know what a Chromosome is (my guess is that an Individual has-a Chromosome)! So I assume you're trying to #include Chromosome.h within Individual.h. So you have a circular dependency.

I suggest exploring this dependency to see it yourself. One (possible) solution that I can't test is: Because the definition for Chromosome only relies on pointers to Individual, you don't need to #include Individual.h inside Chromosome.h.

Just declare it as being a class class Individual; instead, where you used to #include it in the header.
Last edited on
Indeed I have a lot of error messages:

In file included from Individual.h:5,
from Population.h:5,
from GeneticAlgorithm.h:3,
from main.cc:12:
Chromosome.h:8:28: error: expected ‘)’ before ‘*’ token
Chromosome(Individual[]*, int);
~ ^
)
Chromosome.h:16:5: error: ‘Individual’ does not name a type
Individual* sequence;
^~~~~~~~~~
In file included from Population.h:5,
from GeneticAlgorithm.h:3,
from main.cc:12:
Individual.h:11:22: error: expected ‘)’ before ‘,’ token
Individual(string, string);
~ ^
)
Individual.h:27:17: error: ‘string’ has not been declared
void setFactor(string);
^~~~~~
Individual.h:28:20: error: ‘string’ has not been declared
void setOperacion(string);
^~~~~~
Individual.h:29:2: error: ‘string’ does not name a type; did you mean ‘stdin’?
string getFactor();
^~~~~~
stdin
Individual.h:30:2: error: ‘string’ does not name a type; did you mean ‘stdin’?
string getOperacion();
^~~~~~
stdin
Individual.h:31:2: error: ‘string’ does not name a type; did you mean ‘stdin’?
string toString();
^~~~~~
stdin
Individual.h:32:2: error: ‘string’ does not name a type; did you mean ‘stdin’?
string getFormula(vector<Individual>*, int);
^~~~~~
stdin
Individual.h:35:2: error: ‘string’ does not name a type; did you mean ‘stdin’?
string factor;
^~~~~~
stdin
Individual.h:36:2: error: ‘string’ does not name a type; did you mean ‘stdin’?
string operacion;
^~~~~~
stdin
Chromosome.h:8:28: error: expected ‘)’ before ‘*’ token
Chromosome(Individual[]*, int);

Are you trying to pass a 1 dimensional array or 2D? The other parameter makes me think 1D.

Chromosome(Individual[], int);

The rest of the errors are impossible to figure out, you didn't post your Individual.h file.
Last edited on
did you forget to include <string> ? and you need std::string if you don't have the global namespace pulled in (a bad practice for large programs). (Yes I see string in these files, but is it needed & missing in your .h file for individual? I can't tell..)

Again, I think you should grab just the individual class, stuff that in a stub main program and get that to compile. Then try this one again. It will make it easier to fix. We can't help you... looks like we are missing some of the code.
Last edited on
I put the #include so that is not the problem

here is my individual.h

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
#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H
#include <string>
#include "defs.h"
#include "Chromosome.h"

class Individual {

  public:
    Individual(string, string);
      /* takes array of genes, size of chromosome*/

    Individual(int);
      /* takes size of chromosome                */
      /* generates chromosome randomly           */

    ~Individual();

    Chromosome* getChromosome();
    int   getChromosomeLength();
    void  setGene(int, Individual*);
    Individual getGene(int);
    void     setFitness(double);
    double         getFitness();    
    void      printChromosome();
	void setFactor(string);
	void setOperacion(string);
	string getFactor();
	string getOperacion();
	string toString();
	string getFormula(vector<Individual>*, int);

  private:
	string factor;
	string operacion;
	Chromosome* chromosome;
	double fitness;
};

#endif 
You include "Chromosome.h" in "Individual.h" and vice versa. It is a circular include.

Actually you don't need the includes since you use pointer only. A forward reference suffice:
1
2
3
4
5
#include "Chromosome.h"

class Chromosome;
class Individual {
...
I delete the include "Chromosome.h" in "Individual.h" and vice versa, but the problem steel remain
In your "Individual.h" file change every instance of string to std::string. Maybe this will fix the errors, maybe not. It is worth a try.

The problem(s) might result from your "defs.h" file, though.

It is good you are compartmentalizing your code into separate files, once each component works you can no longer bother mucking around with it. Yet during the writing/testing/debugging phases separate files can introduce subtle and not-so subtle errors that can be hard to nail down and squash.

Especially when we don't have everything available to check the code ourselves.

The persistent ‘string’ does not name a type; error could (wild guess) be as simple a fix as using std::string instead of using namespace std; and just string.
Last edited on
Hello, thanks for your advices, I fixed some errors and now I have only two errors:

In file included from Individual.h:6,
from Population.h:5,
from GeneticAlgorithm.h:3,
from main.cc:12:
Chromosome.h:9:28: error: expected ‘)’ before ‘*’ token
Chromosome(Individual[]*, int);
~ ^
)
Chromosome.h:17:5: error: ‘Individual’ does not name a type
Individual *sequence;
^~~~~~~~~~


....
the chromosome.h looks as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CHROMOSOME_H
#define CHROMOSOME_H
#include "defs.h"
#include "Individual.h"

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();
    int* getSequence();
  private:
    Individual* sequence;
    int size;
};
#endif 
Last edited on
Chromosome(Individual[], int);
I remove the * and I have the same problem :(
What are the errors now? It's really hard to diagnose issues for you when we can't see what changes, you know... note that you'd also need to make any corresponding changes to the function in your implementation (.cpp) file as well.

As far as #including the class vs declaring the class before referencing:

• If you need to make a full object of the class, then you need to include the full class definition. This is so that the compiler knows the size of the object at compile-time.
• If you only need to declare a pointer or reference to the object, without dereferencing it, you only need to forward declare the class of the pointer so that the compiler knows that it's a class.
Last edited on
Perhaps if I show you Individual.h, can you sugest me the error

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
#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H
#include <string>
#include <vector>
#include "defs.h"
#include "Chromosome.h"

using namespace std;
class Individual {

  public:
    Individual(string, string);
      /* takes array of genes, size of chromosome*/

    Individual(int);
      /* takes size of chromosome                */
      /* generates chromosome randomly           */

    ~Individual();

    Chromosome* getChromosome();
    int   getChromosomeLength();
    void  setGene(int, Individual*);
    Individual getGene(int);
    void     setFitness(double);
    double         getFitness();    
    void      printChromosome();
	void setFactor(string);
	void setOperacion(string);
	string getFactor();
	string getOperacion();
	string toString();
	string getFormula(vector<Individual>*, int);

  private:
	string factor;
	string operacion;
	Chromosome* chromosome;
	double fitness;
};

#endif 
At the end I put the #include "Chromosome.h" in "Individual.h" and vice versa
Is it really wrong?
I thought in an earlier post you removed the #include "Chromosome.h" from Individual.h?

Do as coder777 said and remove the #include "Chromosome.h" from Individual.h, and replace it with class Chromosome;
I removed now the two #includes and I have the same error
I don't understand that of put class Chromosome in Individual.h
Often times, it helps to copy your work into another project, and reduce all extraneous functionality while still keeping the error that you're experiencing. This can help keep things as a digestible size.

In my case, I'm just going to show a working, but simple, example. Note that this is very much so contrived, just to show you how it looks syntactically.

Since you're using pointers, you need to be aware of who OWNS the pointer. In my example, I am making the Individual OWN the chromosome. I have the individual responsible for creating and destroying the chromosome. I hope that makes sense; I don't know if it does, seeing as I don't know much about biology at a chromosomal level...



Chromosome.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CHROMOSOME_H
#define CHROMOSOME_H

class Individual; // declaring that "Individual" is, in fact, a class.
                  // This is needed to DECLARE a pointer of type Individual

class Chromosome {
  public:
    Chromosome(Individual* sequence);
    Individual* getSequence(); // NOTICE: Not int* (like in your code)
    
  private:
  
    // A Chromosome contains a pointer to an Individual.
    Individual* sequence;
};

#endif // INDIVIDUAL_H 


Chromosome.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Chromosome.h"

Chromosome::Chromosome(Individual* sequence)
{
    this->sequence = sequence;
}

Individual* Chromosome::getSequence()
{
    return sequence;
}


Individual.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H

class Chromosome; // declaring that "Chromosome" is, in fact, a class.

class Individual {
  public:
    Individual();
    ~Individual();
    Chromosome* getChromosome();
    
  private:
    // An Individual contains a pointer to Chromosome (going by your example).
    Chromosome* chromosome;
};

#endif // INDIVIDUAL_H 


Individual.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Individual.h"

#include "Chromosome.h" // because we need to use actual Chromosome objects here, presumably.

Individual::Individual()
{
    chromosome = new Chromosome(this); // again, just an example. Not necessarily best practice here.
}
    
Individual::~Individual()
{
    delete chromosome;
}
    
Chromosome* Individual::getChromosome()
{
    return chromosome;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Individual.h"
#include "Chromosome.h"

int main()
{
    Individual seq;
    
    Chromosome chromo(&seq);
    
    Individual* seq_ptr = chromo.getSequence(); // just doing some operation...
}



Compile with:
g++ -Wall Chromosome.cpp Individual.cpp main.cpp -o out

(Or however you do it through your favorite IDE)
(Note: I just went with .cpp out of habit, .cc is also a valid extension, the compiler doesn't care)

Study this code and see how it all connects. Then, make a copy of your project (so that you don't destroy the original), and simplify it while still keeping the errors that you have.
Last edited on
Topic archived. No new replies allowed.