Namespaces

M

Michael

OK,
I've got a load of classes and functions that deal with Fonts so i want to
wrap them up into a namespace 'FontLib'

Now I have a resource manager that takes a pointer to a FontLibrary,
Now I have forward declared Font Library, but how do i tell the compiler
that the namespace FontLib Exists?


//Resource Mgr.h
class FontLibrary;

using namespace FontLib;



class ResourceMgr

{

public:

ResourceMgr(Kernel*);

~ResourceMgr(void);

FontLib::FontLibrary* pFontLibrary;

}





//Resource.cpp

#include "Fontlibrary.h"

//Implementation







//FontLibrary.h



namespace FontLib

{

class FontLibrary

{

public:

};



}



I get the errors:

c:\Documents and
Settings\Michael\Desktop\Ixium3D\Engine\MeshLibrary\..\ResourceManager\Resou
rceMgr.h(11) : error C2871: 'FontLib' : a namespace with this name does not
exist

c:\Documents and
Settings\Michael\Desktop\Ixium3D\Engine\MeshLibrary\..\ResourceManager\Resou
rceMgr.h(27) : error C2653: 'FontLib' : is not a class or namespace name



How do i tell the compiler that the namespace does actually exist??

Thanks



Mike
 
V

Victor Bazarov

Michael said:
I've got a load of classes and functions that deal with Fonts so i want to
wrap them up into a namespace 'FontLib'

Now I have a resource manager that takes a pointer to a FontLibrary,
Now I have forward declared Font Library, but how do i tell the compiler
that the namespace FontLib Exists?


//Resource Mgr.h
class FontLibrary;

using namespace FontLib;

Forward-declaration is dangerous with namespaces because if somebody
changes the namespace/class relationship in the product (FontLib in your
case, and no matter that it's your own), you will have to search through
all of your code and change it as well. Thus, it's better to have those
forward declarations put in the library itself in a special header and
include that header in your Mgr.h.

The forward declaration header will look like this:

namespace FontLib {
class FontLibrary;
} // namespace FontLib

Of course, you may choose to simply put this text here instead of using
the header/inclusion scheme I suggested. Decide for yourself.
class ResourceMgr

{

public:

ResourceMgr(Kernel*);

~ResourceMgr(void);

FontLib::FontLibrary* pFontLibrary;

}





//Resource.cpp

#include "Fontlibrary.h"

//Implementation







//FontLibrary.h



namespace FontLib

{

class FontLibrary

{

public:

};



} ^
;



I get the errors:

c:\Documents and
Settings\Michael\Desktop\Ixium3D\Engine\MeshLibrary\..\ResourceManager\Resou
rceMgr.h(11) : error C2871: 'FontLib' : a namespace with this name does not
exist

c:\Documents and
Settings\Michael\Desktop\Ixium3D\Engine\MeshLibrary\..\ResourceManager\Resou
rceMgr.h(27) : error C2653: 'FontLib' : is not a class or namespace name



How do i tell the compiler that the namespace does actually exist??

See above

Victor
 
D

David Harmon

On Fri, 14 May 2004 13:55:47 +0000 (UTC) in comp.lang.c++, "Michael"
Now I have forward declared Font Library, but how do i tell the compiler
that the namespace FontLib Exists?

No only that Fontlib exists, but also that class Fontlibrary is in it or
that will be the next thing the compiler will bitch about...

namespace Fontlib
{
class FontLibrary;
}
 
D

David Rubin

Michael said:
OK,
I've got a load of classes and functions that deal with Fonts so i want to
wrap them up into a namespace 'FontLib'
Now I have a resource manager that takes a pointer to a FontLibrary,
Now I have forward declared Font Library, but how do i tell the compiler
that the namespace FontLib Exists?
//Resource Mgr.h
class FontLibrary;

using namespace FontLib;

class ResourceMgr

{

public:

ResourceMgr(Kernel*);

~ResourceMgr(void);

FontLib::FontLibrary* pFontLibrary;
}
^
+-- missing ';'

If ResourceManager is not part of the FontLib namespace, you should do
one of

1) #include "fontlibfwd.h"
which includes forward declarations of FontLib classes in the FontLib
namespace:

namespace FontLib {
class FontLibrary;
// ...
}

2) forward declare in ResourceManager

namespace FontLib {
class FontLibrary;
}
using namespace My;

/david
 
J

J. Andrew MacDonald

Michael said:
OK,
I've got a load of classes and functions that deal with Fonts so i want to
wrap them up into a namespace 'FontLib'

Now I have a resource manager that takes a pointer to a FontLibrary,
Now I have forward declared Font Library, but how do i tell the compiler
that the namespace FontLib Exists?


//Resource Mgr.h
class FontLibrary;

using namespace FontLib;

One other thing, you should avoid placing "using namespace" statements in
headers. Putting these in the headers, will cause anything that includes it to
have the FontLib namespace made "global", which, in my opinion, defeats the
purpose. These statements should only be placed in your source files.
class ResourceMgr

{

public:

ResourceMgr(Kernel*);

~ResourceMgr(void);

FontLib::FontLibrary* pFontLibrary;

}

Looking at this, you've already qualified FontLibrary with the FontLib
namespace, so the "using namespace" statement is not required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top