Click here to Skip to main content
15,868,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code is code for a Vector class that holds 3 points (x, y, z). This code has 2 errors in it and the source of the problem is on line 123 which is inline Vector3 operator *(float k, const Vector3 &v)

If anyone can help me out that would be really wonderful. Thank you for taking your time to read this. God bless you guys.

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;
}
Posted
Comments
Guyverthree 10-Aug-11 12:42pm    
I believe the problem is that you are overloading the unary * operator, hence the too many parameters error that you get when this compiles. I think you want to overload the binary one however I don't know how to make it do this. Sorry that I cannot help you.

however i did find this on the wiki page i was reading

Multiplication a * b Yes Yes T T::operator *(const T& b) const; T operator *(const T &a, const T& b);

it might help you.
Paul Heil 11-Aug-11 10:52am    
As a general 'best practice', it's best to not redefine language keywords. Remove that "#define inline" at the top of your code.

Such an operator must be defined outside of the class to works as intended. It will also typically also need to be friend.
 
Share this answer
 
The operator * cannot have two parameters:
C++
inline Vector3 operator * (float k)
{
  return Vector3(k * x, k * y, k * z); 
}

Do it in this way.
Regards.
 
Share this answer
 
It's worth mentioning that inline is only a hint to the compiler. It is free to inline stuff you don't tell it to, and decide not to inline where you DO tell it to.
 
Share this answer
 
Comments
Paul Heil 11-Aug-11 10:51am    
Not in his code it isn't. He's defined inline to nothingness at the top.
Thank you mbue, that really helped out a lot. I really appreciate this help. Thank you so much!
 
Share this answer
 
Comments
mbue 11-Aug-11 13:05pm    
For the future ;) - use "Add comment" to my post. so i will be notified about your statement.
Regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900