Public Group

# Vector code. namespace problem

This topic is 5418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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;
}



##### Share on other sites
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]

##### Share on other sites
AP:Thanks but it didnt work

My files are like this:

namspace Math
{
class CVector3 {...}
}

cpp file:
namspace Math
{
//function definitions
}

##### Share on other sites
only the header files need to go in the namespace:

namespace MyNamespace
{

class MyClass
{
// functions
};

}

// CPP File

MyClass::SomeFunction
{
}

##### Share on other sites
Do you have "using namespace Math;" at the top of your CPP file?

##### Share on other sites
I tried it like PsyVision said but I got errors saying this:
syntax error before ::' token

So I it like this:

namespace MyNamespace
{

class MyClass
{
// functions
};

}

// CPP File

Math::MyClass::SomeFunction
{
}

and I am back to the same error I had.

##### Share on other sites
here, a working example...

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}

##### Share on other sites
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

• 18
• 29
• 11
• 21
• 16