Jump to content
  • Advertisement
Sign in to follow this  

How to declare

This topic is 4235 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

I have been working on a geometric library that is available here... http://codestash.googlepages.com/ezgeom The problem that I am running into is that in the main.h header file I have... #include "../ezGeom/Lib/clPoint_3d.h" . . . #include "../ezGeom/Lib/clVector_3d.h" So inside the vector class I have no problems using a point because the point class has already been declared. But I want to create a member function in the point class that has a vector as its parameters. Can this be done, do I have to make the function a friend function? Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
You have what is know as a circular dependancy. This can be broken using forward declarations. Lets assume we have classes A and B thus:

// A.h
#include "B.h"
class A
{
private:
B m_a_member_of_type_B; // need to include "B.h" because the compiler needs
// to know how big B is.
};

// B.h
class B
{
public:
void AFunction (class A parameter_of_type_A); // a forward declaration!
// No need to #include "A.h"
};

// B.cpp
#include "A.h"
void B::AFunction (A parameter_of_type_A)
{
// body of function
}

You can also do the following:

// B.h
class A; // a forward declaration!

class B
{
public:
void AFunction (A parameter_of_type_A); // OK to use "A" instead of "class A" since it's been forward declared above.
};

If you're using namespaces, then the correct way to forward declare is:

// C.h
namespace BNamespace
{
class B;
}

class C
{
public:
void Function (BNamespace::B parameter);
};


Skizz

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!