Sign in to follow this  
kelaklub

Forward declaration

Recommended Posts

I have been working on a geometric library, and I have uploaded a stripped down version of the library that is causing a problem for me regarding the use of forward declarations, I think. To compile I have been doing "g++ main.cpp" To download the code... http://codestash.googlepages.com/ezGeom.zip Basically in the point class I have placed a forward declaration to the vector class.
template < typename tType >
class clVector_3d; // Forward declaration


clVector_3d < tType > vector_1;

When I attempt to instantiate an object for the vector class the compiler complains about the isNull() function that is a template specialized implementation for data type long. If I comment out that specific isNull() function, it compiles perfectly. I don't understand why this is happening or what this has to do with the forward declaration. Some advice please.

Share this post


Link to post
Share on other sites
An example of forward declaration is this:

somefile.h


class anotherClass;

class someclass
{
private:

anotherClass* paC;
};


You can only instantiate pointers to types from a forward declaration. To instantiate an object of the type you need to include the types header.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Yeah I read the following article on wikipedia...

http://en.wikipedia.org/wiki/Circular_dependency

But what I don't understand is why everything compiles perfectly when I comment out the "isNull()" function in my point class. The one that is template specialized for datatype LONG. You said that to instantiate an object of the type you need to include the types header. I have not done that in the file containing the point class, and the compiler does not complain.

Share this post


Link to post
Share on other sites
All the source code can be downloade from the following link...

http://codestash.googlepages.com/ezGeom.zip

I'm using gcc so to compile this I just do a "g++ main.cpp" at the command prompt.

And when you compile the code you will get errors, but if you simply comment out the template specialized function called isNull() inside the point class it compiles with no problems.

But the rules apparently for forward declarations is that you can only instantiate pointers to types from a forward declaration. To instantiate an object of the type you need to include the types header.

I have neither included the types header in the point class nor have I instantiated pointers. And yet the only thing the compiler does not like is the template specialized function isNull() for datatype long. Now I have no problem getting rid of this template specialized function, but I would like to know the reason for these errors.

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