I have only 2 errors in my code. Both of them are on Line 123. The errors are:

1>c:\users\zvjezdan\documents\visual studio 2010\projects\week5_math\week5_math\program.cpp(124): error C2804: binary 'operator *' has too many parameters
1>c:\users\zvjezdan\documents\visual studio 2010\projects\week5_math\week5_math\program.cpp(124): error C2333: 'main::Vector3::operator *' : error in function declaration; skipping function body

This is code for finding 3d points (x, y, z) for an object that is held in a Vector class. I am supposed to find the distance between these two objects. These are the only errors I have and I was wondering if anyone can help me out.

Thank you for your guys' time.

Zvjezdan Veselinovic

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdlib>
#define inline

using namespace std;

int main()
{
	class Vector3
	{
	public: float x, y, z;
			Vector3() {}
			Vector3(const Vector3 &a) : x(a.x), y(a.y), z(a.z) {}
			Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz) {}
			Vector3 &operator = (const Vector3 &a)
			{
				x = a.x;
				y = a.y;
				z = a.z;
				return *this;
			}
			bool operator ==(const Vector3 &a) const 
			{
				return x==a.x && y ==a.y && z==a.z;
			}
			bool operator !=(const Vector3 &a) const
			{
				return x != a.x || y != a.y || z != a.z;
			}
			void zero()
			{
				x = y = z = 0.0f;
			}
			Vector3 operator -() const
			{
				return Vector3(-x, -y, -z);
			}
			Vector3 operator +( const Vector3 &a) const
			{
				return Vector3(x + a.x, y + a.y, z + a.z);
			}
			Vector3 operator -( const Vector3 &a) const
			{
				return Vector3(x - a.x, y - a.y, z - a.z);
			}
			Vector3 operator *(float a) const
			{
				return Vector3(x * a, y * a, z * a);
			}
			Vector3 operator /(float a) const
			{
				float oneOverA = 1.0f / a;
				return Vector3(x * oneOverA, y * oneOverA, z * oneOverA);
			}
			Vector3 &operator +=(const Vector3 &a)
			{
				x += a.x;
				y += a.y;
				z += a.z;
				return *this;
			}
			Vector3 &operator -=(const Vector3 &a)
			{
				x -= a.x;
				y -= a.y;
				z -= a.z;
				return *this;
			}
			Vector3 &operator *=(float a)
			{
				x *= a;
				y *= a;
				z *= a;
				return *this;
			}
			Vector3 &operator /=(float a)
			{
				float oneOverA = 1.0f / a;
				x *= oneOverA;
				y *= oneOverA;
				z *= oneOverA;
				return *this;
			}
			void normalize()
			{
				float magSq = x*x + y*y + z*z;
				if(magSq > 0.0f)
				{
					float oneOverMag = 1.0f / sqrt(magSq);
					x *= oneOverMag;
					y *= oneOverMag;
					z *= oneOverMag;

				}
				else if(magSq < 0.0f)
				{
					cout << "There has got to be an error." << endl;
					cout << endl;
					normalize();
				}
			}
			float operator *(const Vector3 &a) const 
			{
				return x * a.x + y * a.y + z * a.z;
			}
	
	inline float vectorMag(const Vector3 &a)
	{
		return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
	}
	
	inline Vector3 crossProduct(const Vector3 &a, const Vector3 &b)
	{
		return Vector3( a.y * b.z - a.z * b.y, 
			            a.z * b.x - a.x * b.z, 
			            a.x * b.y - a.y * b.x);
	}
	
	inline Vector3 operator *(float k, const Vector3 &v)
	{
		return Vector3(k * v.x, k * v.y, k * v.z); 
	}

	inline float distance( const Vector3 &a, const Vector3 &b)
	{
		float dx = a.x - b.x;
		float dy = a.y - b.y;
		float dz = a.z - b.z;
		return sqrt(dx * dx + dy * dy + dz * dz);
	}
	};

	cout << endl;
	system("pause");
	return 0;
}

On line 123 make it a friend function.

inline friend Vector3 operator *(float k, const Vector3 &v)

This won't work if you have your class defined within main() but I'm not too sure why you have it like that in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.