Sign in to follow this  
webwraith

It's right there!

Recommended Posts

Right, not entirely sure what's going on with this. I can't work out why my Object and Zone classes are unable to find the Polygon class (despite it being there above them) when the Object class is picked up fine by the Zone class. The code;
struct Vector
{
    float x,y,z;
};

class Vertex
{
    Vector position,normal;
};

class Material//contains a diffuse(texture)map,a normal(bump)map, a specular map, and a sound effect for objects moving across it
{
    Texture *diffuse,*normal,*specular;
};

class Polygon
{
        Vertex* index_list;
        size_t  num_indices;
        Material* material;
};

class Object
{
        Polygon*    polygon_list;
        Object*     children;
        size_t      num_polys,num_children;
};

class Zone
{
        Object* object_list;
        Polygon* polygon_list;
        size_t num_objects,num_tris;

    public:

        void AddObject(Object*,size_t);
        void RemoveObject(Object*);
};

class Level
{
        Zone*       zone_list;
        Material*   material_list;
        size_t      num_zones,num_materials;

     public:

        void LoadLevel();
};
and the errors;
level.h:28: error: ISO C++ forbids declaration of `Polygon' with no type // <-Object
level.h:28: error: expected `;' before '*' token
level.h:36: error: ISO C++ forbids declaration of `Polygon' with no type // <-Zone
level.h:36: error: expected `;' before '*' token

I've tried deleting the object files(not suprisingly didn't do anything, as this is in a header), removing and re-adding the file to the project, and resetting the computer, but I'm out of ideas. Is it just a coding problem that's glaringly obvious to others, or am I justified in being stumped? Oh, and before I forget, Texture is defined in another header, linked in before this one.

Share this post


Link to post
Share on other sites
Quote:
Original post by webwraith
It seems there is a variable called Polygon somewhere else.

This would not have happened with strict coding conventions (variables start with a lowercase letter, types with an uppercase letter).

Share this post


Link to post
Share on other sites
Aiyayayayaya. Don't do this:


class Thingy {
Subthingy* subthingies; // will point to a new Subthingy[] array
int num_subthingies;
};

// Lots of code somewhere else that manages the memory


Do this:


class Thingy {
std::vector<Subthingy> subthingies; // the standard library loves you
};

// The "num_subthingies" is always available as subthingies.size()
// and you will never have to worry about the pointer being uninitialized or NULL.

Share this post


Link to post
Share on other sites
#DevFred: What are you talking about, I'm defining the class name as Polygon, not instantiating an object, which is exactly what you seem to be getting on at me for NOT doing. If it makes you feel any better, I always use the convention you have decided is best for yourself. At least we should be able to read each others code.

#Zahlman: In case you didn't notice it, there is a class with name "Vector" in my code, already. Now I know the STL and CSL vectors are both lower case, but a single difference is too close for me. Besides as long as I'm careful, there is no reason why the "pointer and max count" method won't work. It was used all the time back in C, after all...


...it's also probably the way the std::vector takes care of things under the hood, anyway...

Share this post


Link to post
Share on other sites
Except std::vector will do it right for you. With your method you will have to spend time and effort making sure you do it right instead. This includes managing allocation and destruction, resizing, validity and aliasing, et cetera.

std::vector is the safe, idiomatic C++ method of handling dynamic (relative to compile time -- which yours is, even if max size stays fixed at runtime) arrays in C++. Learn to use it, and you become a better C++ programmer.

Or don't, and insist on being a C programmer in a language that is not C, with all the disadvantages and productivity hurdles that entails.

Share this post


Link to post
Share on other sites
Quote:
In case you didn't notice it, there is a class with name "Vector" in my code, already. Now I know the STL and CSL vectors are both lower case, but a single difference is too close for me.
Don't let the names of the SC++L classes discourage you from using them. They're all safely wrapped in the std namespace, so unless you're doing something unwise, you shouldn't have any collisions.
Quote:
Besides as long as I'm careful, there is no reason why the "pointer and max count" method won't work. It was used all the time back in C, after all...
But why make things harder for yourself than necessary?

Share this post


Link to post
Share on other sites
Quote:
Original post by webwraith

Besides as long as I'm careful, there is no reason why the "pointer and max count" method won't work. It was used all the time back in C, after all...


Why are you using such verbose syntax....


#define class C
#define size_t INT
#define float REAL
C VCTR { REAL X,Y,Z; }
C VRTX { VCTR POS,NML; }
C MTRL { TXTR *DFS,*NML,*SPL; }
C PLGN { VTX *IDX; INT NMI; MTRL MTR1; }
C OBJ { PLGN PLLS; OBJ * CHLD; INT NMPL,NMCL; }
C ZONE { OBJ *OBLS; PLGN *PLLS; INT NMOB,NMTR;
void AOBJ(OBJ*,INT);
void ROBJ(OBJ*);
}
C LEVL { ZONE *ZNLS; MTRL *MTLS; INT NMZN,NMMT;
void LLVL();
}


What?

It worked in BASIC, and Fortran before then. And if you're careful, you won't confuse MTRL and MTR1, or run into name conflicts.

Use what was proven 40 years ago to work and work well. Stand on the shoulders of giants. And don't make me bring out punch card reader....

Share this post


Link to post
Share on other sites
If the OP ever decides to use SSE for his vector class, then everything will not work if he uses vector, as it does not keep alignment, he'll have to change all the classes where he has arrays of Vectors into pointers with sizes.

Also, he was talking about C, not C++.

Finally, as long as he manages things nicely, it's not much harder to deal with pointers, and you can get better performance (not completely sure, maybe it also depends on the platform).

You're are more experienced than me, so I'd love to see some explaination to use vector or the STL at all when creating arrays of vectors.

I do agree with you on all other situations which include non-performance sensitive code (code that isn't going to be used in a very tight loop, where nanoseconds can add up into miliseconds, or more depending on the application.

Share this post


Link to post
Share on other sites
Quote:
If the OP ever decides to use SSE for his vector class, then everything will not work if he uses vector, as it does not keep alignment, he'll have to change all the classes where he has arrays of Vectors into pointers with sizes.
I'm not sure this is true, but I don't know enough about SSE and so on to say for sure. Perhaps someone else can comment.
Quote:
Also, he was talking about C, not C++.
The OP's code looks to me like it's written in C++.
Quote:
Finally, as long as he manages things nicely, it's not much harder to deal with pointers...
Managing a dynamic array manually is considerably more verbose and error-prone than using a container such as std::vector. A decent programmer will probably be able to manage it, of course, but again, why make things harder than they need to be?
Quote:
...and you can get better performance...
I don't know about that. With any and all range- and error-checking turned off, the performance of std::vector should be equivalent to that of built-in arrays (at least in terms of element access).

Is there a particular reason you think a vector will not perform as well as a built-in array?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this