Sign in to follow this  
3dmodelerguy

namespace problem

Recommended Posts

3dmodelerguy    160
ok i have this class: Vector.h:
#ifndef VECTOR_H
#define VECTOR_H

#include <math.h>
namespace NMath
{
	class CVector3
	{
	public:
		CVector3( float x = 0.0f, float y = 0.0f, float z = 0.0f )
		{
			_x = x;
			_y = y;
			_z = z;
		}

		CVector3 operator+=( const CVector3 &vector )
		{
			return *this = ( *this + vector );
		}

		CVector3 operator+( const CVector3 &vector )
		{
			return CVector3( vector._x + _x, vector._y + _y, vector._z + _z );
		}

		CVector3 operator-=( const CVector3 &vector )
		{
			return *this = ( *this - vector );
		}

		CVector3 operator-( const CVector3 &vector )
		{
			return CVector3( _x - vector._x, _y - vector._y, _z - vector._z );
		}

		CVector3 operator*=( float number )
		{
			return *this = ( *this * number );
		}

		CVector3 operator*( float number )
		{
			return CVector3( _x * number, _y * number, _z * number );
		}

		CVector3 operator/=( float number )
		{
			return *this = ( *this / number );
		}

		CVector3 operator/( float number )
		{
			return CVector3( _x / number, _y / number, _z / number );
		}

		CVector3 operator-()
		{
			return CVector3( -_x, -_y, -_z );
		}

		float Dot( CVector3 &vector )
		{
			return CVector3( _x * vector._x, + _y * vector._y, + _z * vector._z );
		}

		CVector3 operator*( CVector3 &vector )
		{
			return CVector3( _y * vector._z - _z * vector._y, _z * vector._x - _x * vector._z, _x * vector._y - _y * vector._x, );
		}

		float Lenght()
		{
			return sqrt( _x * _x, _y * _y, _z * _z );
		}

		bool operator!=( const CVector3 &vector )
		{
			return ( vector._x == _x, vector._y == _y, vector._z == _z );
		}

		bool operator==(const CVector3 &vector )
		{
			return ( vector._x != _x, vector._y != _y, vector._z != _z );
		}

		CVector3 Normalize()
		{
			float lenght = Lenght();

			x /= lenght;
			y /= lenght;
			z /= lenght;

			return *this;
		}

		float Distance( CVector3 &vector )
		{
			float distance_x = vector._x - _x;
			float distance_y = vector._y - _y;
			float distance_z = vector._z - _z;

			return sqrt( distance_x * distance_x, distance_y * distance_y, distance_z * distance_z );
		}

		float _x;
		float _y;
		float _z;

	};
}
#endif 

and this is my main.cpp:
#include "Vector.h"

#include <iostream>

void PrintV( NMath::CVector3 &v );

void main()
{
	NMath::CVector3 *a = new NMath::CVector3( 1, 2, 3 );
	NMath::CVector3 *b = new NMath::CVector3( 3, 8, 4 );
	NMath::CVector3 *c = new NMath::CVector3( 3, 4, 5 );
	NMath::CVector3 *d = new NMath::CVector3( 3, 2, 1 );

	PrintV( a + b );
	PrintV( b - a );
	PrintV( b / a );
	PrintV( a * 5 );
	
	getch();

	delete a, b, c, d;
}

void PrintV( NMath::CVector3 &v )
{
	std::cout << v._x << " " << v._y << " " << v._z std::endl;
}

now for a reason i don't know i am getting these errors:
------ Build started: Project: vectortest, Configuration: Debug Win32 ------

Compiling...
main.cpp
c:\Documents and Settings\ryan\My Documents\Visual Studio Projects\vectortest\main.cpp(5) : error C2653: 'NMath' : is not a class or namespace name
c:\Documents and Settings\ryan\My Documents\Visual Studio Projects\vectortest\main.cpp(5) : error C2065: 'CVector3' : undeclared identifier
c:\Documents and Settings\ryan\My Documents\Visual Studio Projects\vectortest\main.cpp(5) : error C2065: 'v' : undeclared identifier
c:\Documents and Settings\ryan\My Documents\Visual Studio Projects\vectortest\main.cpp(5) : error C2182: 'PrintV' : illegal use of type 'void'
c:\Documents and Settings\ryan\My Documents\Visual Studio Projects\vectortest\main.cpp(5) : fatal error C1903: unable to recover from previous error(s); stopping compilation

Build log was saved at "file://c:\Documents and Settings\ryan\My Documents\Visual Studio Projects\vectortest\Debug\BuildLog.htm"
vectortest - 5 error(s), 0 warning(s)


---------------------- Done ----------------------

    Build: 0 succeeded, 1 failed, 0 skipped

I have been working on a bood size OpenGL project and been using classes and namespace just like this but for some reason this on is not working, anyone now why?

Share this post


Link to post
Share on other sites
SiCrane    11839
I find it hard to believe that this is your real code since the header itself fails to compile do to a number of errors in many different places. If you remove those parts of the header that fail to compile then the main.cpp file does compile cleanly. I'm guessing the reason that you are having troubles lies in the parts that you didn't include in the header.

Share this post


Link to post
Share on other sites
3dmodelerguy    160
yea, i think i was the stl's vector becuase i guessing that they have the same ifnedef like mine does so i just added a 3 to the end everything compiles plus i did fix some of the mistakes in my vector code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this