-
Wierd compiler error.
I've been working on a project for a while now and everything's been working fine until I added one last class, at which point I started to get the following errors:
--------------------Configuration: TestingGrounds - Win32 Debug--------------------
Compiling...
Base.cpp
c:\c++\tester\testinggrounds\base.h(58) : error C2143: syntax error : missing ';' before '*'
c:\c++\tester\testinggrounds\base.h(58) : error C2501: 'Polygon' : missing storage-class or type specifiers
c:\c++\tester\testinggrounds\base.h(58) : error C2501: 'Poly' : missing storage-class or type specifiers
c:\c++\tester\testinggrounds\base.cpp(181) : error C2065: 'Poly' : undeclared identifier
c:\c++\tester\testinggrounds\base.cpp(192) : error C2541: delete : cannot delete objects that are not pointers
Testground.cpp
c:\c++\tester\testinggrounds\base.h(58) : error C2143: syntax error : missing ';' before '*'
c:\c++\tester\testinggrounds\base.h(58) : error C2501: 'Polygon' : missing storage-class or type specifiers
c:\c++\tester\testinggrounds\base.h(58) : error C2501: 'Poly' : missing storage-class or type specifiers
Error executing cl.exe.
TestingGrounds.exe - 8 error(s), 0 warning(s)
----------------------
I'm using msvc++ 6 and here is the code:
Code:
//base.h
#ifndef BASE_H
#define BASE_H
#include "math2.h"
/////////////FACE//////////////////////////////////
typedef struct tagFACE //The face of a poly
{
VERTEX *Vertex; //List of verts for storing position
VERTEX Normal; //For setting up lights
GLfloat tX,tY; //Tex-coords
}FACE;
//////////////POLYGON CLASS////////////////////////////
///////////////////////////////////////////////////////
class Polygon
{
private:
int NumFaces; //The number of faces the poly consist of
FACE *Faces; //The faces
int NumVertices; //Number of "total" vertices, ie verts with same coordinates don't count more than once
VERTEX *SharedVert; //The shared vertices
VERTEX Facing; //Vertex describing what direction the poly is facing
public:
//Constructors
Polygon();
Polygon(int nF,int nV,VERTEX f);
//Destructor
~Polygon();
//Methods, self-explanatory
int GetNumFaces(); //Gets the number of faces
VERTEX GetFacing(); //Gets the direction the poly is facing
VERTEX *GetVertices();//Returns an array containing all the shared vertices
void CreateSharedVert(VERTEX v);//Create one shared vert
void CreateSetofSharedVerts(int,VERTEX *v);//Create int nr of shared verts *v
bool ModSharedVert(int which,VERTEX v);//Modify which vert to be v
bool SetFace(int which,GLfloat x,GLfloat y, int numverts,int *whichverts,VERTEX norm);//Modify face which. Must send an int array containing info on which verts it will use and in what order
void CreateFace(GLfloat x,GLfloat y,int nv,int *wv,VERTEX norm);//Creates a new face
};
////////////////////BASE ACTOR CLASS/////////////////////////
/////////////////////////////////////////////////////////////
class Actor
{
private:
int PolyCount; //The number of polys
Polygon *Poly; //A dynamic array of the polys that is the actor
VERTEX Facing; //A vertex describing the direction the actor is facing
VERTEX PointOfMass;//Where the actors point of mass is located
public:
Actor(); //Standard constructor
~Actor(); //Standard destructor
int GetPolyCount();//Returns the amount of polys
VECTOR GetFacing();//Returns the facing
VERTEX GetMPoint();//Returns the actor's point of mass
};
#endif
//base.cpp - for readability, excluded what I think is working stuff
#include "base.h"
Actor::~Actor()
{
if(Poly)
{
delete [] Poly;
Poly=NULL;
}
}
It's the beginning of what will (hopefully) be a 3d-engine. It's not very functional yet, though. I left out some stuff in the code that I think is irrelevant. Especially since commenting all references to "Polygon" objects in "Actor" makes the project compilable and functional (except that I don't get any polys...)
I've scoured the net and a host of programming forums for a working solution to this, and I've tried to double-check every single little piece of code, but can't find anything... I'll be very thankful for any help.
Thanks in advance!
-
Can you specify where the errors are reffering to - is it in your class declaration or somewhere where you are trying to create an instance of the Polygon obj?
You might have to explicitly specify and set the Polygon ptr with the new keyword.
Code:
class Actor
{
private:
int PolyCount; //The number of polys
Polygon *Poly; //A dynamic array of the polys that is the actor
EXAMPLE:
Code:
*Poly = new Polygon();
*I am assuming you have included all include files for the Polygon obj in all files that need the polygon obj.*
-
Sorry, I forgot to specify where the errors occured...
They all occur where I refer to "Polygon". No matter what way or where that happens.
There's nothing wrong with the array, as I would dynamically create it in "Actor"s (as of yet unexistant) constructor. Like this:
Code:
Poly=new Polygon[PolyCount];
But the lack of a constructor is not the error. I can "//" the entire actor-class and all its methods etc. and still get the errors when trying to create a polygon, no matter where it's created.
-
The errors are telling you that it doesn't reconginze Polygon as an obj when you are trying to instantiate it.
Most commonly this occurs due to the right header files have not been included.
-
Well... All of the header files are included. I've checked that countless of times. Besides I get an error if creating a polygon in the same header that "polygon" is in. And in it's cpp. I've even tried including all headers in use in every .cpp and .h, to no avail. I've tried creating a new project and adding the files to it and dozens of other things. I just can't seem to get it to work.
-
Just noted another thing that makes it even weirder: I can use anything else that were declared and defined in that header with no errors at all. Logically that would mean that there's something wrong with the Polygon-class, but I can't find anything wrong with it...
-
Do you have Polygon class in it's own .cpp and .h?
-
Hard to tell from your posted code but it looks like several classes are declared in one header???
If this is the case, it might be that Actor class or another class is trying to call an instance of Polygon before it is fully defined. (before the functions and constructors have been defined in the cpp).
Try changing the order around in the cpp, such as fully defining the Polygon functions before defining the functions in the actor class.
This is why it's good practice to keep all Obj's in there own .h and .cpp files. Defining and Declaring multiple obj's in one file can create problems, especially when they rely on each other. Keeping them seperated allows you to just #include the header files neccesary to make that obj work when it requires other objs.