forward declaration

Started by
4 comments, last by Nemesis2k2 18 years, 8 months ago
Could someone tell me how to do a forward declaraction of a struct named Triangle? I thought it was just: struct Triangle; However, it isn't working. Here's what I have: struct Triangle; void addtriangles(vector<Triangle> triangles) { Triangle t; t = Triangle(buildvector(1.13337, -1.06298, 1.97633), buildvector(1.31295, -1.11932, 1.69197), buildvector(1.42413, -0.926698, 1.82076)); t.setnormal(0.571062, -0.657906, 0.490966); triangles.insert(triangles.end(), t); t = Triangle(buildvector(1.31295, -1.11932, 1.69197), buildvector(1.43575, -1.04969, 1.56764), buildvector(1.42413, -0.926698, 1.82076)); t.setnormal(0.69935, -0.62975, 0.338119); triangles.insert(triangles.end(), t); t = Triangle(buildvector(1.38998, -1.11479, 1.26084), buildvector(1.43575, -1.04969, 1.56764), buildvector(1.31295, -1.11932, 1.69197)); t.setnormal(0.559517, -0.823771, 0.0913353); } The compiler says: c:\lavarock\triload4.h(5) : error C2079: 't' uses undefined class 'Triangle' Also, when I use class instead of struct, it still doesn't work. Any ideas? mike http://www.coolgroups.com/forum
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Advertisement
You need to define the Triangle structure, a simple forward declaration (which you otherwise did perfectly) won't do here.
Why is it that a forward declaration does not suffice in this case?

mike
http://www.coolgroups.com/forum
Mike C.http://www.coolgroups.com/zoomer/http://www.coolgroups.com/ez/
Because you are creating and using an object. Obviously, the compiler can't know how to generate the code for what you're trying to do if it doesn't know anything about its type. About the only things you can do with some forward-declared type T are:

  • Declare an extern variable of type T

  • Declare and/or define a pointer to T

  • Declare and/or define a reference to T

  • Use T as a template argument

  • Create a typedef alias for T


There are probably more, but those are the most important ones.
It does not work because the complier has no way of knowing what variables are declared in Triangle, which means it does not know how large (in bytes) Triangle will be, so it does not know how much memory to reserve. This means you can have and work with pointers to the Triangle structure. You won't be able to call any methods, or use any variables that you later declare.

class K;...K * ThisIsOkay;ThisIsOkay->ButNotAnymore();


EDIT: Beaten. :D
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
Any peice of code that needs to:
-Access a member of a given class (using . or ->)
-Use the scope resultion operator on a given class (using ::)
-Create an object of the given type, either on the heap or the stack
-Recieve an object of the given type by value

Cannot make do with a forward declaration alone. Likewise, any class definition which:
-Inherits from the given class
-Contains an object of the given class as a member (not a pointer or a reference, the object itself)

Requires a full definition of the class to be provided. The reason for this is simply that in these situations, without a full definition being provided beforehand, the compiler does not have enough information to know what code to generate.

This topic is closed to new replies.

Advertisement