Vector code. namespace problem

Started by
6 comments, last by snk_kid 19 years, 7 months ago
I wasnt having this problem with my vector code until I put it inside of a namespace now the function below keeps giving this error: line:71 Vector.cpp syntax error before `{' token

    CVector3 CVector3::CrossProduct(CVector3 v1, CVector3 v2)
    {
        CVector3 Normal;
    
         // Calculate the cross product with the non communitive equation
         Normal.x = ((v1.y * v2.z) - (v1.z * v2.y));
         Normal.y = ((v1.z * v2.x) - (v1.x * v2.z));
         Normal.z = ((v1.x * v2.y) - (v1.y * v2.x));
         
         return Normal;
     }



______________________________________________________________________________________With the flesh of a cow.
Advertisement
The vector class itself is in the namespace? You might need to put on the scope then, like

namespace::CVector3

[/source]
namespace::CVector3 CVector3::CrossProduct(namespace::CVector3 v1, namespace::CVector3 v2)
{
namespace::CVector3 Normal;

// Calculate the cross product with the non communitive equation
Normal.x = ((v1.y * v2.z) - (v1.z * v2.y));
Normal.y = ((v1.z * v2.x) - (v1.x * v2.z));
Normal.z = ((v1.x * v2.y) - (v1.y * v2.x));

return Normal;
}[/source]
AP:Thanks but it didnt work

My files are like this:

header:
namspace Math
{
class CVector3 {...}
}

cpp file:
namspace Math
{
//function definitions
}

______________________________________________________________________________________With the flesh of a cow.
only the header files need to go in the namespace:


// Header File

namespace MyNamespace
{

class MyClass
{
// functions
};

}



// CPP File

MyClass::SomeFunction
{
}
PsYvIsIoN
Do you have "using namespace Math;" at the top of your CPP file?
I tried it like PsyVision said but I got errors saying this:
syntax error before `::' token

So I it like this:

// Header File

namespace MyNamespace
{

class MyClass
{
// functions
};

}



// CPP File

Math::MyClass::SomeFunction
{
}

and I am back to the same error I had.
______________________________________________________________________________________With the flesh of a cow.
here, a working example...

header file "vecMath.h"
namespace math{  class vector3 {   public:     float a,b,c;     void copy(float a, float b, float c);  };};


CPP file
#include "vecMath.h"using namespace math;void vector3::copy(float a, float b, float c){   // do copy}
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
First for a 3-vector you'll wont to have the functions inlined so you'll be better off defining them with-in the header instead.

Secondly your cross-product member function is alittle strange, because it will end-up like so:

CVector3 v, u, w;

CVector3 n = v.CrossProduct(u, w);

for a member function it should be the cross-product of the instance in question and another vector.

Thirdly you to won't pass your vectors by reference & constant reference instead of by value.

so with that in mind i would do something like this:

#ifndef _VECTOR3_#defeine _VECTOR3_namespace math {   template < typename T >   struct vector3 {      T x, y, z;      //la-la-la            vector3<T> cross(const vector3<T>&);      //la-la-la   };   template < typename T >   inline vector3<T> vector3<T>::cross(const vector3<T>& u) {      /* --- insert code --- */   }};#endif

This topic is closed to new replies.

Advertisement