Jump to content
  • Advertisement
Sign in to follow this  
directNoob

[C++]#include"" problem!

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

Good evening to all. I have two classes, CVector3 and CVector4. Now I want to convert between them. I mean, I need to overload the cast operator for both. Something like this:
CVector3 vc3 ;
...
CVector4 vc4 = (CVector4)vc3 ;

...
----
...
CVector4 vc4 ;
...
CVector3 vc3 = (CVector3)vc4 ;

But the problem is, how can I include CVector3 in CVector4 and vice versa to have each other known. I tried to put the prototypes of the classes into separate .h files and included only this file, but then the compiler doesnt find the files where everything is defined! What can I do due to this problem? Grateful Alex

Share this post


Link to post
Share on other sites
Advertisement
Dont use the include file. Do:

// tell the compiler CVector3 is a class.
class CVector3;

class CVector4
{
...
// defined in the .cpp file, where CVector3 _is_ included
CVector4(const CVector3&);
};

Share this post


Link to post
Share on other sites
Quote:
I tried to put the prototypes of the classes into
separate .h files and included only this file, but then the compiler doesnt find
the files where everything is defined

Did you try like the following?
Are you using any instances of the forward declared classes not as pointers or references in the headers?


vector3 header

class Vector4;//forward declare

class Vector3
{
};

vector 3 cpp
#include "vector3.h"
#include "vector4.h"

vector4 header

class Vector3;//forward declare

class Vector4
{
};

vector 4 cpp
#include "vector4.h"
#include "vector3.h"



Share this post


Link to post
Share on other sites
Quote:
Original post by incin
Dont use the include file. Do:

// tell the compiler CVector3 is a class.
class CVector3;

class CVector4
{
...
// defined in the .cpp file, where CVector3 _is_ included
explicit CVector4(const CVector3&);
};


Fixed ;).

Share this post


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

Fixed ;).


Matter of opinion if you ask me. But true, the explicit should be there if you want the explicit casting as in the example.

Share this post


Link to post
Share on other sites
Don't provide casting operators for things like this. It will lead to problems, allowing operations you don't want to allow, and will cause ambiguity errors where there shouldn't be ambiguity. Instead, provide explicit casting functions for the situations where you actually want to convert from one type to another.

Share this post


Link to post
Share on other sites
Hi and thanks.

First of all.
The original problem is still noteworthy,
because I encountered it once and I'm sure to
meet it again, so it must work...

Ok, now it works. :)
@dmail
I made it like this. I'm writing my little math lib
and there I have more files than just CVector3 ~4.
This means, I put all prototypes into one file, called,

3DMath_Prototypes.h

Than I put this header to all .h files involved into my 3d math lib.
But I have forgotten to put the needed .h files into the .cpp files.
For Example:
In CVector3.h, I included 3DMath_Prototypes.h.
But I have forgotten to include CVector4.h to CVector3.cpp, and
this means, that the compiler just has found the forward decl within
3DMath_Prototypes.
Very logically. I could have solved this myself!

But thanks for your help and your time.

But Sneftel revealed another problem.
@Sneftel:
Could you please explain why it is not good to have a nice cast operator
overloaded? And with "explicit casting functions" you mean somthing like
Vec3ToVec4(...) ?

Greetings
Alex

Share this post


Link to post
Share on other sites
It's not good because it can allow operations you don't want to allow. In the case of an automatic cast from Vector3 to Vector4, for example, you might end up being able to pass a Vector3 into a constructor which doesn't make sense with anything except a full Vector4. Additionally, since there's no single way to convert a Vector3 to a Vector4, there's no cast operator that'll make sense all the time. Using a free function like that for casting allows you to specify the fourth component.

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!