Jump to content
  • Advertisement
Sign in to follow this  
mike74

forward declaration

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
You need to define the Triangle structure, a simple forward declaration (which you otherwise did perfectly) won't do here.

Share this post


Link to post
Share on other sites
Why is it that a forward declaration does not suffice in this case?

mike
http://www.coolgroups.com/forum

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!