• Advertisement
Sign in to follow this  

Type not registering?

This topic is 2230 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'm working on a game engine for a game I'm working on, and in doing so, I needed some vector types, and a MathHelper namespace.

vector2 and vector3 are in the namespace BCEngine, and they are both in their own header files.

What vector2 basically looks like:

vector2.h

namespace BCEngine
{
struct vector2
{
double X,Y;
//All the other stuff here:
};
};


Then I have mathhelper.h:

mathhelper.h

#include "vector2.h"
namespace MathHelper
{
//The compiler ignores "BCEngine::vector2" and pretends that it's not found.
BCEngine::vector2 GetZero();
};


When I compile it, I get an error like "vector2" is not a member of "BCEngine". Does anyone have any clue why it's doing this and how I can fix it?

Share this post


Link to post
Share on other sites
Advertisement
Either the code you showed us is not the code you're actually working with or there's ANOTHER vector2.h somewhere in your include paths that the compiler pulls in instead of the one you want.

P.S. If you're using Visual Studio, you can configure the compiler to print out the path name of each included file during a compile run. That way you can see exactly what is getting included. (Configuration Properties -> C/C++ -> Advanced -> Show Includes)

2nd P.S. Under Configuration Properties -> C/C++ -> Preprocessor -> Preprocess to a file you can even make the compiler generate a file showing you exactly what code is emitted by the preprocessor. Open that file and see if you can find the definition of your vector2 struct. If you can't then for some reason it's not getting included.

Share this post


Link to post
Share on other sites

Either the code you showed us is not the code you're actually working with or there's ANOTHER vector2.h somewhere in your include paths that the compiler pulls in instead of the one you want.

P.S. If you're using Visual Studio, you can configure the compiler to print out the path name of each included file during a compile run. That way you can see exactly what is getting included. (Configuration Properties -> C/C++ -> Advanced -> Show Includes)


I figured out the problem. Apparently the compiler was starting the compilation at "vector3.cpp", and "vector3.cpp" only included "vector3.h" and "math.h". I fixed it so that it will includes the proper files and it compiles.

Share this post


Link to post
Share on other sites

Apparently the compiler was starting the compilation at "vector3.cpp" [...]


What do you mean, "it started the compilation at"? Each translation unit (i.e. each .cpp file) always is compiled separately. What you include in one translation unit has no effects whatsoever on any of the other translation units. So if you have two .cpp's that both use vector2 then they both need to include vector2.h.

Share this post


Link to post
Share on other sites

[quote name='Nyxenon' timestamp='1331852665' post='4922419']
Apparently the compiler was starting the compilation at "vector3.cpp" [...]


What do you mean, "it started the compilation at"? Each translation unit (i.e. each .cpp file) always is compiled separately. What you include in one translation unit has no effects whatsoever on any of the other translation units. So if you have two .cpp's that both use vector2 then they both need to include vector2.h.
[/quote]

"BCEngine.h" tied all the headers together in order, so when it starts compiling at "vector3.cpp" (which only include "vector3.h" and "math.h", it compiled the whole thing in an order in which somehow "mathhelper.h" was compiled before "vector2.h", which means that vector2 wasn't a valid type yet.

Share this post


Link to post
Share on other sites

I'm working on a game engine for a game I'm working on, and in doing so, I needed some vector types, and a MathHelper namespace.

vector2 and vector3 are in the namespace BCEngine, and they are both in their own header files.

What vector2 basically looks like:

vector2.h

namespace BCEngine
{
struct vector2
{
double X,Y;
//All the other stuff here:
};
};


Then I have mathhelper.h:

mathhelper.h

#include "vector2.h"
namespace MathHelper
{
//The compiler ignores "BCEngine::vector2" and pretends that it's not found.
BCEngine::vector2 GetZero();
};


When I compile it, I get an error like "vector2" is not a member of "BCEngine". Does anyone have any clue why it's doing this and how I can fix it?


My guess is that given that you are not using typedef to do the type registration, in your code you would need to do:



#include "vector2.h"
namespace MathHelper
{
//The compiler ignores "BCEngine::vector2" and pretends that it's not found.
struct BCEngine::vector2 GetZero();
};


Either that or in the header file do:

vector2.h

namespace BCEngine
{
typedef struct str_vector2
{
double X,Y;
//All the other stuff here:
} vector2;
};

Share this post


Link to post
Share on other sites

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

"BCEngine.h" tied all the headers together in order, so when it starts compiling at "vector3.cpp" (which only include "vector3.h" and "math.h", it compiled the whole thing in an order in which somehow "mathhelper.h" was compiled before "vector2.h", which means that vector2 wasn't a valid type yet.

[/font]
[/quote]
Your build is brittle. In addition, forcing every file to include this "master" file will increase your build times, because if you make a change in a header file all clients will need to be rebuilt, rather than just the ones that actually depend on it.

You shouldn't have a master header which is required to get the right order. Instead, each header file should compile cleanly if it were included in an otherwise empty source file (the typical way of doing this is to include the header for a given source file as the very first line in the source file associated with that header).

Share this post


Link to post
Share on other sites

My guess is that given that you are not using typedef to do the type registration, in your code you would need to do:

You seem very confused by the C++ language.

Share this post


Link to post
Share on other sites

[quote name='Rekai' timestamp='1331891496' post='4922535']
My guess is that given that you are not using typedef to do the type registration, in your code you would need to do:

You seem very confused by the C++ language.
[/quote]

I may be, I never used namespaces, but gcc/g++ never let me do

struct name{
type1 field1;
...
typen fieldn;
};

name variable;


It complains and you need to declare the variable as:

struct name variable;


And I don't see why namespaces would change that

Share this post


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

  • Advertisement