Using your own namespace for the first time

J

jimmy

Hi,

Have tried my best but can't seem to get it.

I have a Matrix and a Vector class that I would like to be in a
Geometry namespace. More detail...

****** Matrix.h ******
#include "Vector.h" // this class uses Vector
****** Matrix.cpp ****
#include "Matrix.h" **
#include "Vector.h" **

****** Vector.h ******
****** Vector.cpp ****
#include "Vector.h"

****** Geometry.h ****
// include this file whenever using geometry
#include "Matrix.h"
#include "Vector.h"

Pleeeease help... (will post in a sec what I have tried)

-Jimmy
 
J

jimmy

Ok so I managed to get something going.

1. Wrap Matrix.h and Vector.h in namspace Geometry {}
2. Paste Geometry:: before all the definitions in Matrix.cpp and
Vector.cpp

Is this ok "style/convention"?

Additional question:
****** Matrix.cpp ****
Matrix Geometry::Matrix::foo() {...} // Doesn't compile
Geometry::Matrix Geometry::Matrix::foo() {...} // Compiles!
void Geometry::Matrix::foo(Matrix m) {..} // Compiles!

Why don't I need to qualify Matrix m?

-Billy
 
J

jimmy

Ok so I managed to get something going.


1. Wrap Matrix.h and Vector.h in namspace Geometry {}
2. Paste Geometry:: before all the definitions in Matrix.cpp and
Vector.cpp


Is this ok "style/convention"?


Additional question:
****** Matrix.cpp ****
Matrix Geometry::Matrix::foo() {...} // Doesn't compile
Geometry::Matrix Geometry::Matrix::foo() {...} // Compiles!
void Geometry::Matrix::foo(Matrix m) {..} // Compiles!


Why don't I need to qualify Matrix m?


-Jimmy
 
L

Larry I Smith

jimmy said:
Hi,

Have tried my best but can't seem to get it.

I have a Matrix and a Vector class that I would like to be in a
Geometry namespace. More detail...

****** Matrix.h ******
#include "Vector.h" // this class uses Vector
****** Matrix.cpp ****
#include "Matrix.h" **
#include "Vector.h" **

****** Vector.h ******
****** Vector.cpp ****
#include "Vector.h"

****** Geometry.h ****
// include this file whenever using geometry
#include "Matrix.h"
#include "Vector.h"

Pleeeease help... (will post in a sec what I have tried)

-Jimmy

Read this:

http://gethelp.devx.com/techtips/cpp_pro/10min/10min1099.asp

Regards,
Larry
 
J

jimmy

Thanks, but do you have an idea about this:

****** Matrix.cpp ****
Matrix Geometry::Matrix::foo() {...} // Doesn't compile
Geometry::Matrix Geometry::Matrix::foo() {...} // Compiles!
void Geometry::Matrix::foo(Matrix m) {..} // Compiles!
void Geometry::Matrix::bar(Vector v) {...} // Compiles!

Why don't I need to qualify Matrix m or Vector v? (It's certainly nice
cuz I have to change less but I'm shocked I'm aloud to do this)

-Jimmy
 
L

Larry I Smith

jimmy said:
Thanks, but do you have an idea about this:

****** Matrix.cpp ****
Matrix Geometry::Matrix::foo() {...} // Doesn't compile
Geometry::Matrix Geometry::Matrix::foo() {...} // Compiles!
void Geometry::Matrix::foo(Matrix m) {..} // Compiles!
void Geometry::Matrix::bar(Vector v) {...} // Compiles!

Why don't I need to qualify Matrix m or Vector v? (It's certainly nice
cuz I have to change less but I'm shocked I'm aloud to do this)

-Jimmy

I don't know what the C++ spec says, but I would guess that
since you qualified 'foo' completely, the compiler is matching
it to the correct class and therefore knows what namespace
the functions arg belong to (i.e. Geometry is implied for
the function args, since that matches a valid signature for
the fully qualified Geometry::Matrix::foo). Whether this
a standard, or unique to your compiler, I don't know.

Regards,
Larry
 
J

Jonathan Mcdougall

Ok so I managed to get something going.
1. Wrap Matrix.h and Vector.h in namspace
Geometry {}
2. Paste Geometry:: before all the definitions in
Matrix.cpp and Vector.cpp

Is this ok "style/convention"?

Have a look:

// matrix.h

namespace geometry
{
class matrix
{
public:
void f();
};
}

// matrix.cpp
# include "matrix.h"

namespace geometry
{

void matrix::f()
{
// ...
}

}


// main.cpp
# include "matrix.h"

int main()
{
geometry::matrix m;
m.f();
}
Additional question:
****** Matrix.cpp ****
Matrix Geometry::Matrix::foo() {...} // Doesn't
compile

Matrix (which is really ::Matrix in your case) does not exists. Only
Geometry::Matrix does.
Geometry::Matrix Geometry::Matrix::foo() {...} //
Compiles!
Yep.

void Geometry::Matrix::foo(Matrix m) {..} //
Compiles!

Same thing as above.
Why don't I need to qualify Matrix m?

Because the function is part of the class. Therefore, it would be
redundant to fully qualify the arguments, since the function is already
fully qualified.

You should take the habit of defining non inline member functions
inside the namespace too (as I did above), it is clearer and you'll get
less errors.
 
J

jimmy

"You should take the habit of defining non inline member functions
inside the namespace too"

Done. Thank you very much. -j
 
I

Ioannis Vranos

jimmy said:
Thanks, but do you have an idea about this:

****** Matrix.cpp ****
Matrix Geometry::Matrix::foo() {...} // Doesn't compile
Geometry::Matrix Geometry::Matrix::foo() {...} // Compiles!
void Geometry::Matrix::foo(Matrix m) {..} // Compiles!
void Geometry::Matrix::bar(Vector v) {...} // Compiles!

Why don't I need to qualify Matrix m or Vector v? (It's certainly nice
cuz I have to change less but I'm shocked I'm aloud to do this)


The first defines a member function foo of class Matrix which is defined in namespace
Geometry, which returns an object of another class named Matrix in the global namespace.

The second defines a member function foo of class Matrix which is defined in namespace
Geometry, and returns an object of the same class Matrix. This is equivalent to:

namespace Geometry
{
Matrix Matrix::foo() {...}
}


In cases 3 and 4, the compiler is performing namespace resolution by itself (I so not
recall the correct terminology). In other words it is checking the namespace in which the
member functions belong to.
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top