# Static library and namespaces

## Recommended Posts

Hello. I'm making a math library and I would like to ude a namespace for it. but when I implmented it i got around 500 errors. program\microsoft visual studio 9.0\vc\include\codeanalysis\sourceannotations.h(234) : error C3083: 'vc_attributes': the symbol to the left of a '::' must be a type I don't know why?? I guess there is something with static library settings or something. anyways this is how i included the namespaces //Header files
//the main header, no class

#ifndef _MYMATH_H_
#define _MYMATH_H_
namespace MyMath
{
//forward declarations
class CVector3;
class CVector4;
class CMatrix4;
class CQuaternion;

#include "math.h"
#include "CVector3.h"
#include "Vector4.h"
#include "CMatrix4.h"
#include "CQuaternion.h"
}
#endif //_MYMATH_H_

#ifndef _CMATRIX4_H_
#define _CMATRIX4_H_
#include "MyMath.h"

namespace MyMath
{
//!  CMatrix4 Class.
/*!
CMatrix4 is row major matrix class for 3D programming
*/
class ECMatrix4
{
..........
};
}

#ifndef _CVECTOR3_H_
#define _CVECTOR3_H_
#include "MyMath.h"

namespace MyMath
{
//!  CVector3 Class.
/*!
CVector3 is a 3D vector class for 3D programming
*/
class CVector3
{
.....
};
}
//end  vector3

and I do this in very class
Cpp files are the same

#include "StdAfx.h"
#include "MyMath.h"

namespace MyMath
{
......cpp function etc
}


Do I have to do something special to make namespaces work when dealing with a static libraty. MSDN show how to use name spaces in a static library project in VS 2005. Don't see why id don't work in VC 2008. //HermanssoN [Edited by - HermanssoN on November 7, 2008 1:16:38 AM]

##### Share on other sites
I don't have an answer to your question (at least not without looking more closely), but I have a couple of questions/suggestions:

1. Edit your original post and enclose your code in [source] tags; this will make it easier to read.

2. It would be better to use a different convention for your header guards; symbols that begin with an underscore followed by a capital letter are reserved by the implementation.

3. What does the 'C' prefix in your class names stand for?

It does look like you might have some strange (perhaps circular) inclusions going on there - perhaps that's where the errors are coming from.

##### Share on other sites

CMyClass, C stand for class, a quite normal prefix?

Ib'e always used _CLASSNAME_H_ for my include guards, but i supose I could use

all classes you see declared in the main header include "MyMath.h" in their h/cpp file.

The error only occure when I added the namespaceif. If I remove the namepace tags from the code I can use the library in other apps. I just world really like to have the nampespace there

##### Share on other sites
FWIW. Do not use double underscores (i.e. __SOMETHING__). Double underscores are reserved by the compiler and should not be used. It is better to not use any underscores, or better yet use pragma once since its supported by virtually every compiler out there.
#pragma oncenamespace MyMath{ //... stuff}

Note: You can use both include guards and pragma once if you want, but often its redundant.
#pragma once#ifndef HEADER_H#define HEADER_Hnamespace MyMath{ //... stuff}#endif /* HEADER_H */

##### Share on other sites
I think the problem is that you are including you other headers inside you namespace. You should put your #includes BEFORE or AFTER namespace MyMath:

#ifndef MYMATH_H_#define MYMATH_H_namespace MyMath{	//forward declarations	class CVector3;	class CVector4;	class CMatrix4;	class CQuaternion;}#include <cmath> // don't use math.h, it's C header file, C++ has cmath#include "CVector3.h"#include "Vector4.h"#include "CMatrix4.h"#include "CQuaternion.h"#endif //MYMATH_H_

##### Share on other sites
Thnx..good to know.

##### Share on other sites
Found the error, I included "math.h" inside my name space

Now i hit a new error. I lost my Quaternions.

if i have one name space there i include header

#pragma oncenamespace MyMath{ #include "Vector3.h" etc}

//Vector3 header#pragma once#include "MyMath.h"namespace MyMath{ class Vector3{}; etc}

//Vector3 cpp#pragma once#include "MyMath.h"namespace MyMath{Vector3::Vector3( void ){}etc

This should make Vector3 a part of namespace MyMath, right?
And if I do this with every class that I would like to include in my math library all classes can declare varibles of each other as long as they share the same namespace?

[Edited by - HermanssoN on November 7, 2008 8:08:05 AM]

##### Share on other sites
Quote:
 Original post by HermanssoNCMyClass, C stand for class, a quite normal prefix?

What's wrong with just MyClass?

Prefixes stem from C which lacks namespaces and stricter code organization. Use of CName is abundant on web simply because many C++ developers transitioned from C.

Quote:
 Ib'e always used _CLASSNAME_H_ for my include guards, but i supose I could use __CLASSNAME_H__ instead

Double underscores are reserved for the compiler.

Why not just use CLASSNAME_H? Or NAMESPACE_CLASSNAME_H? Or, if you fear conflicts CLASSNAME_H_CURRENTDATEANDTIME.

Leading underscores are commonly seen in code examples, but they all violate the standard in same way and aren't really needed.

If conflicts become a problem, it's much better that they conflict with your own code in predictable way, than with compiler in unknown way.

##### Share on other sites
Remember, #inlcude is a copy and paste operation. This is what the compiler sees, after preprocessing:
namespace MyMath{    namespace MyMath    {        class Vector3        {        };    }}

Thus Vector3 ends up in MyMath::MyMath. And because Vector3.h can be included on its own, you can potentially end up with MyMath::Vector3 and MyMath::MyMath::Vector3. This is not a good idea.

The correct solution:
// mymath.h#ifndef MYMATH_H#define MYMATH_H// note: absence of namespace#include "vector3.h"#include "more_maths.h"// more includes...#endif// vector3.h#ifndef MYMATH_VECTOR3_H#define MYMATH_VECTOR3_H// don't include "mymath.h"namespace MyMath{     struct Vector3     {         // ...     };}#endif// and so on for other files

##### Share on other sites
The problem is that vector3 will have funtions that uses a matrix, matrix will have functions that that uses both vectors and quaternions.

MY original soultion was to include all math classes in MyMayh.h and also make forward declarations there.

as an example if i include Vector.h and quaternion.h in matrix.h
and include matrix.h in vector.h, won't I have a bunch of headers that get included several times? or does the include guards make sure that i won't end up with multiple includes of the same header

##### Share on other sites
You're making the same mistake again. NEVER include headers inside namespaces, unless you do some preprocessor tricks. #include "vector.h" means: insert here the text stored in vector.h.

##### Share on other sites
Also beware of running afoul of this.

##### Share on other sites
I supose I could create all classes in one header file, all classes will have acces to all other classes and only one header is neaded.

// mymath.h#ifndef MYMATH_H#define MYMATH_H namespace MyMath{class Vector3;class Matrix4;class Quaternion;class Vector3{//Vector3 stuff};class Matrix4{//Matrix4 stuff};class Quaternion{//Quaternion stuff};}#endif

But if I for some reason realy need to have the different classes declared in separat headers and all classes need instances of each other. Obviously the more math classes hat performes operations using othe classes the more troublesome it gets.

##### Share on other sites
Quote:
 Original post by HermanssoNI supose I could create all classes in one header file, all classes will have acces to all other classes and only one header is neaded.*** Source Snippet Removed ***But if I for some reason realy need to have the different classes declared in separat headers and all classes need instances of each other. Obviously the more math classes hat performes operations using othe classes the more troublesome it gets.
Having the different classes access each other directly shouldn't be a problem, as long as you pass by reference and use forward declarations. I suspect the problem is that you still have your files organized incorrectly.

Also, remember that you can use non-member functions as well as member functions. A function that, say, converts a quaternion to a matrix is a good candidate for a non-member function. Furthermore, making it a non-member function can (potentially) eliminate the matrix class' dependency on the quaternion class.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628281
• Total Posts
2981800

• 10
• 11
• 17
• 14
• 9