-
|
|
C++ Undeclared Identifier!
|
Hello! I'm learning C++, and I'm trying to make a game in DirectX, so I hope this is the right forum!
I've created an absolutely horrible compiler error (a set of errors) that just don't make sense, so I need some help!
Here are some of the errors: error C2065: 'Pea' : undeclared identifier 34 peas error C2059: syntax error : '>' 34 peas fatal error C1004: unexpected end-of-file found 34 peas
and it repeats those same errors 3 times.
Anyway, here is the offending code.. This is grid.h... | #pragma once | | | #include <vector> | | #include "BoyLib/Vector2.h" | | #include "Boy/Graphics.h" | | #include "Boy/ResourceManager.h" | | #include "Box2D.h" | | #include "Pea.h" | | | | [snip] | | | | //The grid class draws the tileset grid. | | class Grid | | { | | public: | | Grid(Boy::ResourceManager *rm, b2World *world, std::vector<Pea *> peas); | | ~Grid(); | | | | void draw(Boy::Graphics *g); | | | | [snip] |
and this is Pea.h... | #pragma once | | #ifndef PEA_H | | #define PEA_H | | | #include "Boy/ResourceManager.h" | | #include "BoyLib/Vector2.h" | | #include "Boy/Graphics.h" | | #include "Grid.h" | | #include "Box2D.h" | | #define GRAVITY 5.0f | | | | [snip] | | | | //controls pea physics and movement, along with AI. | | class Pea | | { | | public: | | Pea(BoyLib::Vector2 position, b2World *world); | | ~Pea(); | | | | void update(float dt); | | b2Vec2 getPosition() | | { | | return mPosition; | | } | | void setPeaState(PeaState state); | | PeaState getPeaState() | | { | | return mState; | | } | | private: | | PeaState mState; | | b2Vec2 mPosition; | | b2Body *mBody; | | b2World *mWorld; | | }; | | #endif | Any ideas? I'm utterly stumped why it won't read Pea.h and find the definition.
Graphics Programmer,Sandswept Studios.http://www.sandswept.net
|
|
-
|
|
Re: C++ Undeclared Identifier!
|
where dose the error point at? so we can start somewhere.
but the only thing i can see is this
class Grid
{
public:
Grid(Boy::ResourceManager *rm, b2World *world, std::vector<Pea *> peas);
~Grid();
void draw(Boy::Graphics *g);
}; <-------------------- You missed this.
but i suspect thats a copy to the forum error.
Whow, thats a big memory leak... It sure is! and we are sinking!
kartflygaren675@hotmail.com
|
|
-
|
|
Re: C++ Undeclared Identifier!
|
I've not missed the closing "};" part. :( here is the full Grid.h definition... | #pragma once | | | #include <vector> | | #include "BoyLib/Vector2.h" | | #include "Boy/Graphics.h" | | #include "Boy/ResourceManager.h" | | #include "Box2D.h" | | #include "Pea.h" | | | | enum TileType | | { | | None = 0, | | Normal = 1, | | LeftRamp = 2, | | RightRamp = 3, | | Gel = 4, | | Spring = 5, | | Flag = 6 | | }; | | | | struct Tile | | { | | TileType type; | | BoyLib::Vector2 position; | | b2Body *body; | | }; | | | | //The grid class draws the tileset grid. | | class Grid | | { | | public: | | Grid(Boy::ResourceManager *rm, b2World *world, std::vector<Pea *> peas); | | ~Grid(); | | | | void draw(Boy::Graphics *g); | | | | void setTileType(TileType type, int x, int y); | | TileType getTileType(int x, int y); | | | | Tile getTile(int x, int y); | | | | void toWorld(BoyLib::Vector2 *point); | | void toGrid(BoyLib::Vector2 *point); | | | | const static int mWidth = 15; | | const static int mHeight = 11; | | float mCellWidth; | | float mCellHeight; | | private: | | void ensureRange(int *x, int *y); | | | | Tile mTiles[mWidth][mHeight]; | | Boy::Image *mImages[7]; | | Boy::Image *mImageShadow; | | | | std::vector<Pea*> mPeas; | | b2World *mWorld; | | }; | Also i get the following errors when i try to compile... error C2065: 'Pea' : undeclared identifier at line 32 error C2059: syntax error : '>' at line 32 error C2059: syntax error : ')' at line 32 error C2143: syntax error : missing ';' before '}' at line 32 fatal error C1004: unexpected end-of-file found at line 32
the problem seems to be caused by this line, but I can't see anything wrong with it or the definition :S
Grid(Boy::ResourceManager *rm, b2World *world, std::vector<Pea *> peas);
It works if i remove the std::vector<Pea *> part of the constructor. But I need that bit for my game to work :P Thanks for the help... C++ is so weird.
Graphics Programmer,Sandswept Studios.http://www.sandswept.net
|
|
-
|
|
Re: C++ Undeclared Identifier!
|
are you sure its not definde like this <Pea>*peas ?
Whow, thats a big memory leak... It sure is! and we are sinking!
kartflygaren675@hotmail.com
|
|
-
|
|
Re: C++ Undeclared Identifier!
|
I have defined it to be like that so that std::vector<Pea *> holds pointers to the pea objects, which are on the heap. :S
The compiler lets me define std::vector<Pea *> elsewhere in my code.
I tried changing it to std::vector<Pea>* but the compiler still complains about Pea being undeclared. :(
Edit:
I fixed it! I removed the #include <Pea.h> and wrote class Pea; instead. The compiler stops complaining!
Graphics Programmer,Sandswept Studios.http://www.sandswept.net
|
|
-
-
- (453)
-
premium membership
-
Posts
84
|
Re: C++ Undeclared Identifier!
|
You have a problem of include loops maybe?
if you're just using Pea as a pointer why don't you try to forward declare it and then in the .cpp use it properly. It's the "elegant" and actually efficient (in compiling time) way to do it:
Grid.h:
....
class Pea;
class Grid
{
...
std::vector<Pea*> m_Peas;
};
Grid.cpp
...
#include "Pea.h"
...
// do something with my vector of peas
m_Peas.push_back(new Pea());
...
|
|
-
-
- (453)
-
premium membership
-
Posts
84
|
Re: C++ Undeclared Identifier!
|
Oh, I forgot to tell you. If you forward declare the class you don't need to include it in Grid.h
You should really look for some good books on C++ programming. I'm sorry I can't recommend you one now, but there are lots out there.
|
|
-
|
|
Re: C++ Undeclared Identifier!
|
I have a few books on C++ programming; Accelerated C++ and a Deitel C++ How To Program. Their examples and explanations are very nice right up until you got horribly stuck on problems like this!
I'll know to use forward declarations for next time though. I did wonder if an include loop was causing the problem, but i'm using the #pragma once declaration at the top of each header file... I thought that it stops include loops from occurring.
Graphics Programmer,Sandswept Studios.http://www.sandswept.net
|
|
-
-
- (453)
-
premium membership
-
Posts
84
|
Re: C++ Undeclared Identifier!
|
To be honest I learnt C++ at the uni and I've improved a lot since by reading articles on the internet, discussing with people, etc... I think that code reviews, discussing your code, etc., helps a lot, so maybe what you need is just that, a bit of experience.
Regarding the #pragma once, I'd have thought the same, but I'd recommend you always use both the #pragma and the #ifndef ... #define ... at the beginning of the file. Not all the compilers support the #pragma once directive. Anyway, Microsoft compiler usually supports that directive so I don't know...
Anyway, I hope the forward declaration solves it for you. It will also save you loads of compilation time if you use it properly: you should really avoid #includes in .h files unless they are completely necessary. That and a good use of precompiled headers speed up incredibly the compilation times.
Finally, just an advise if you mind me. Use namespaces, they help a lot when organizing the code properly. It's really simple but it helps you divide your classes better. Maybe you are already using them and just avoiding them in these files because they are the main program or something, but just in case.
Happy coding!
|
|
-
-
- (453)
-
premium membership
-
Posts
84
|
Re: C++ Undeclared Identifier!
|
Oh, BTW, if you're using already namespaces, check you haven't forgotten to close any of them because that may cause that very same error of unexpected identifier... It's a really nasty mistake since the compiler won't give you much info on the subject, but since you are already in a namespace, classes from other namespaces are not accessible as normally.
|
|
-
|
|
Re: C++ Undeclared Identifier!
|
I'm currently at uni but my comp. sci course mainly focuses on Java programming, but they did do a few lectures on C++. I can code in Java and C# very well, and C++ has very similar syntax, so I agree that this is just an experience thing.
I am using the Microsoft C++ Compiler that comes with Visual Studio 2008 which I'm pretty sure supports the #pragma once directive. Oh well, I'll be making a lot more use of forward declarations in header files since you don't need to worry about the internals of the class!
How do precompiled headers work?
In this case I'm not using namespaces since this is the main program. The game library I'm using makes use of namespaces though. Do you recommend using namespaces all of the time, including your main program code?
This advice is great, thanks! :D Do you have any more advice on good practice in C++?
Graphics Programmer,Sandswept Studios.http://www.sandswept.net
|
|
-
-
- (1121)
-
premium membership
-
Posts
229
|
Re: C++ Undeclared Identifier!
|
Scott Meyer's "Effective C++" series
Effective C++
More Effective C++
Effective STL
and Herb Sutters "Exceptional C++" series
Exceptional C++
More Exceptional C++
Exceptional C++ Style
have tons of good advice on C++ best practices.
I'd start with the Scott Meyers books first.
Three colors, endless fun ... Primary Attack
|
|
-
|
|
Re: C++ Undeclared Identifier!
|
Nice advice.. I've bought the Effective C++ book off amazon, so hopefully that will help me improve things!
Graphics Programmer,Sandswept Studios.http://www.sandswept.net
|
|
|