Jump to content
  • Advertisement
Sign in to follow this  
Ainokea

Vector code. namespace problem

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
AP:Thanks but it didnt work

My files are like this:

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

cpp file:
namspace Math
{
//function definitions
}

Share this post


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


// Header File

namespace MyNamespace
{

class MyClass
{
// functions
};

}



// CPP File

MyClass::SomeFunction
{
}

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!