[C++]#include"" problem!

Started by
6 comments, last by Sneftel 16 years, 11 months ago
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
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&);
};
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 headerclass Vector4;//forward declareclass Vector3{};vector 3 cpp#include "vector3.h"#include "vector4.h"vector4 headerclass Vector3;//forward declareclass Vector4{};vector 4 cpp#include "vector4.h"#include "vector3.h"
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 ;).
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.

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.
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
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.

This topic is closed to new replies.

Advertisement