Reputation: 192
So I have a few syntax errors that are saying:
Error C2143 syntax error: missing ';' before '* '
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2238 unexpected token(s) preceding ';'
Error C2143 syntax error: missing ';' before '*'
All of these are in:
#pragma once
#include "World.h"
class Organism
{
protected:
int strength;
int initiative;
int age, x, y;
char sign;
World *world; //line that makes errors
public:
Organism(World*,int,int);
virtual ~Organism();
virtual void action() = 0;
virtual void collision() = 0;
virtual char getSign() = 0;
};
I have also these errors(yeah, twice same error):
Error C2061 syntax error: identifier 'World'
Error C2061 syntax error: identifier 'World'
In line with Organism(World*,int,int); (I do not know how to add line numbers on StackOverflow). What can cause these problems?
Here is the World.h code:
#pragma once
#include "Organism.h"
class World
{
int height;
int width;
Organism*** organisms;
public:
World();
World(int, int);
~World();
void DrawWorld();
void NextRound();
};
Upvotes: 1
Views: 1383
Reputation: 36617
Your problem is circular dependencies. That is reflected in your header files each including each other, so the definition of World
depends on the definition of Organism
which depends on the definition of World
, ad infinitum.
The (non-standard) #pragma once
might stop the header files from infinitely including each other, but does not change the fact that the types defined within them have that recursive dependence.
You need to break the dependency. A common technique is to use forward declarations, and avoid having (at least) one of the headers relying on the other, and use declarations to break dependencies between the classes.
#ifndef ORGANISM_H_INCLUDED // include guard instead of #pragma once
#define ORGANISM_H_INCLUDED
class World; // declaration of World
class Organism
{
// as you have it
};
#endif
and
#ifndef WORLD_H_INCLUDED
#define WORLD_H_INCLUDED
class Organism;
class World
{
// as you have it
};
#endif
The important thing to realise is that, given a class declaration (not a definition) it is only possible to work with pointers or references.
To implement member functions of both classes that rely on knowledge of both full class definitions you will need to #include
both headers. Based on that, your code will be able to do things like create instances of either type.
Upvotes: 0
Reputation: 409432
It's because the "Organism.h"
header file depends on the "World.h"
header file which depends on the "Organism.h"
and so on in infinity. It's a so-called circular dependency.
In your case it's very easy to break, since neither header file, as you show them, actually need the definitions of the other classes, only the declarations.
That means the World.h
header file can look like this instead:
#pragma once
// Note: No #include directive here
class Organism; // Forward-declaration of the class
class World
{
int height;
int width;
Organism*** organisms;
public:
World();
World(int, int);
~World();
void DrawWorld();
void NextRound();
};
The same can be done with the Organism.h
header file.
The source file that uses the classes needs the full definitions of the classes, so they need to include both header files.
Upvotes: 8