   namespaces and classes
  Hello ppl!I would like to encapsulate a few loosely related classes into a namespace.  For example:// MathLib.hnamespace MathLib{  class Point{...};  class Matrix{...};  class Frame{...};}No problemo.  But those component classes are quite large, and the comments take up many lines so I want to separate the classes into their own files.  I used the notion of "extending" namespaces to produce the following:// MathLib.h#include "Point.h"#include "Matrix.h"#include "Frame.h"namespace Mathlib{}[\code]and each of those classes are aritten as follows:// Frame.hnamespace MathLib{  class Frame  {  private:    // etc...  public:    // etc...  }}Up to here it's been pretty much smooth sailing.  All three classesa are def'd the same way.   My whole scheme falls apart when I try to include, say, Matrix as a private member of Frame, i.e.// Frame.hnamespace MathLib{  class Frame  {  private:    Matrix matrix;  public:    // etc...  }}The code above does not recognize Matrix as a type. error C2146: syntax error : missing ';' before identifier 'matrix'If I write instead "MathLib::Matrix matrix;", I get a different error -error C2039: 'Matrix' : is not a member of 'MathLib'I've tried changing the order of the #includes in MathLib.h, but nothing seems to be working.The reason I want to do this is that each of those objects have some functions associated with them that aren't really member functions, but are intimately related (operators and the like).  Also, an API I'm using (Solidworks) has a "Frame" type already, so my code looks silly with ::Frame all over the place when I want to reference my own Frame.I haven't really written any namespaces before this, so maybe I'm missing something?  To summarize, I'd like some those classes to be members of others, and I'd like to have them (plus some global methods operating interchangably on those types) all wrapped up into one nice, neat namespace requiring a single #include and maybe some "using"s on the client pages.I'm going for something like#include <iostream>using namespace std;Any ideas? Thanks for readingP.S. I'm working with unmanaged C++ in Visual Studio 2005.
  Just noticed I was leving off the ';' after the definition of class Frame.  That mistake is NOT in my code, it was just a typo here.thanks again   ;  I don't see why it shouldn't work if you're sure you include the header files in the right order.If class A uses class B, you need to include the header file for class B before defining class A. In that case I'd usually let the class A header file include the class B header file.In case two classes are refering eachother you'd need to do a forward declaration for one of them, i.e. "class Blabla;" somewhere at the start.Also it's a good idea to put some include guards in your header files so you don't end up including the same twice (#pragma once or #ifndef BLABLA #define BLABLA, etc)//Matrix.h#ifndef __MATRIX_H__#define __MATRIX_H__namespace MathLib {  class Matrix {    public: // Blabla  };}#endif//Frame.h#ifndef __FRAME_H__#define __FRAME_H__#include "Matrix.h"namespace MathLib {  class Frame {    public: // Blabla    private: Matrix m_Matrix;  };}#endif[Edited by - rneckelmann on April 19, 2010 3:18:30 PM];  All the headers are #pragma once'd, and Matrix.h is #included in Frame.h (not necessary though)The classes per se aren't referencing each other (Matrix is a member of Frame, the opposite is untrue).  I still #include Matrix before I #include Frame in MathLib.h.#pragma once#include "MathLib.h"using MathLib::Matrix;namespace MathLib // extend MathLib{class Frame{private:Matrix matrix;//Point point;public:Frame(void);~Frame(void);};}Tha code on line 7.. when it's there, I get \frame.h(4) : error C2039: 'Matrix' : is not a member of 'MathLib'\frame.h(4) : error C2873: 'Matrix' : symbol cannot be used in a using-declaration\frame.h(11) : error C2146: syntax error : missing ';' before identifier 'matrix'and when it's commented:\frame.h(11) : error C2146: syntax error : missing ';' before identifier 'matrix'Logically, it shouldn't need to be there since it's being used in MathLib-scope.Again, I'm not sure if this is how namespace extension should be done, I'm hard-pressed to find good reference material on the topic.  For completeness, here's MathLib.h:#pragma once#include "Matrix.h"#include "Point.h"#include "Frame.h"namespace MathLib{}If I add class Matrix;to the namespace body in that file, the compiler thinks that is my class definition and complains about an undefined class (as it should).;  *correction* line 4 not line 7   ;  Seems like you're including MathLib.h from Frame.h, and MathLib.h tries to include Frame.h?Try including only Matrix.h from Frame.h instead.   ;  ...same effect!   ;Quote:Original post by robot ears...same effect!And you don't include MathLib.h from Matrix.h?   ;Quote:I'm not sure if this is how namespace extension should be doneNamespaces are inconsequential here. Every header includes everything it needs.Quote:namespace MathLib {class Frame {  Matrix matrix;  Point point;This header must include Point and Matrix, but not mathlib headers.Quote:I'm going for something like#include <iostream>This has nothing to do with namespaces, or anything else. iostreams are a huge mess, so ios_fwd is often recommended to avoid huge bloat.To minimize dependencies, one should only include exactly what one needs. So if someone wants to work with Frame, they'll include frame.hpp.Or, if compilation times don't matter (such as triggering full rebuild if only a single line in Frame is changed), then single header works://mathlib.hpp#pragma once#include "frame.hpp"#include "point.hpp"#include "matrix.hpp"With this approach, each of the header files is standalone and can be used in isolation. Each of them includes *everything* it needs, the mathlib is merely a convenience header which groups them together.To test dependencies, I use dummy include files. I do something like this: for every header files, there is a dummy cpp file, which contains a single line: #include "foo.hpp"Since each cpp is a compilation unit, this ensures that each header is usable on its own, without hidden dependencies that might arise from include chain.Whether this is kosher C/C++ or not could be argued. Many projects make implicit dependencies based on order of includes, or sometimes even linking order, but my personal opinion is that it is simply not worth the frustration of doing a single line change, and suddenly having half of project fall apart due to hidden include. So I prefer to do the Java or C# approach, where each header is standalone.If these dependencies grow too large, forward includes can still be used.   ;  you nailed it!MathLib.h is included in Frame.h, but NOTHING is included in Matrix.h!  As per usual, MathLib.h includes everything. You will get a big thumbs-up.  The rest of this post is for reference:How to extend namespaces across files:The "parent" header// MathLib.h#pragma once#include "Matrix.h"#include "Point.h"#include "Frame.h"namespace MathLib{}Some classes that are part of the namespace:// Point.h#pragma oncenamespace MathLib{class Point{public:Point(void);~Point(void);};}another class...// Matrix.h#pragma oncenamespace MathLib{class Matrix{public:Matrix(void);~Matrix(void);};}Now another namespace class that has one of the other classes as a member:#pragma once// Frame.h#include "Matrix.h"namespace MathLib {class Frame{private:Matrix matrix;public:Frame(void);~Frame(void);};}A source file would look like:// Frame.cpp#include "Frame.h"typedef MathLib::Frame FrameFrame::Frame(){}Frame::~Frame(){}And a main file would look like// main.cppusing MathLib::Frameint main(){Frame f1;return 0;}[/endcode]