Sign in to follow this  
Ainokea

Vector code. namespace problem

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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:

// Header File

namespace MyNamespace
{

class MyClass
{
// functions
};

}



// CPP File

Math::MyClass::SomeFunction
{
}

and I am back to the same error I had.

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
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

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