Error: ‘Class’ Does Not Name A Type C++, &#39Var&#39 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:

Đang xem: Does not name a type c++

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 ; #endif
does not name a type – C++ Forum
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

Read more: How To Say You Re Welcome In Russian & You'Re Welcome, Thank You In Russian

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() Chromosome* Individual::getChromosome() int Individual::getChromosomeLength() void Individual::setGene(int offset, Individual *gene) Individual Individual::getGene(int offset) void Individual::setFitness(double fit) double Individual::getFitness() void Individual::printChromosome() void Individual::setFactor(string factor) void Individual::setOperacion(string operacion) string Individual::getFactor() string Individual::getOperacion() string Individual::toString() string Individual::getFormula(vector<Rama>* ram, int n) else } private: string factor; string operacion; }
Last edited on May 6, 2019 at 2:03pm UTC

May 6, 2019 at 2:08pm UTC
jonnin (8751)
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 May 6, 2019 at 2:09pm UTC

May 6, 2019 at 2:37pm UTC
Ganado (5818)
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()
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 May 6, 2019 at 2:40pm UTC

May 6, 2019 at 3:02pm UTC
psosmol (71)
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

May 6, 2019 at 3:08pm UTC
Furry Guy (2732)
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 May 6, 2019 at 3:12pm UTC

May 6, 2019 at 3:11pm UTC
jonnin (8751)

Read more: Most Expensive Vendor Item In Wow, What Is The Most Expensive World Of Warcraft Item

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.

Leave a Comment