I am having an issue using this new operator function that I learned. The program thinks that I am calling the operator=() instead of the operator+() so it is hitting an infinite loop.

I marked the areas where there are issues, and i commented some of the newer things that I added just to eliminate any possible issues.

It is on line 36 in the first code segment and line 43 on the last code segment provided.

NOTE: I do need this operator=() for the rest of my file, I just need to know how to override the = with the +..... ty :)

// vect.h -- Vector class with <<, mode state
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
	private:
		double x; // horizontal value
		double y; // vertical value
		double mag; // length of vector
		double ang; // direction of vector
		char mode; // 'r' = rectangular, 'p' = polar
		// private methods for setting values
		void set_mag();
		void set_ang();
		void set_x();
		void set_y();
	public:
		Vector();
		Vector(double n1, double n2, char form = 'r');
		void set(double n1, double n2, char form = 'r');
		~Vector();
		double xval() const {return x;} // report x value
		double yval() const {return y;} // report y value
		double magval() const {return mag;} // report magnitude
		double angval() const {return ang;} // report angle
		void polar_mode(); // set mode to 'p'
		void rect_mode(); // set mode to 'r'
		// operator overloading
		Vector operator+(const Vector & b) const;
		Vector operator-(const Vector & b) const;
		Vector operator-() const;
		Vector operator*(double n) const;
		Vector operator=(const Vector & b) const; // This is causing the issue
		Vector operator/(const int b) const;
		// friends
		friend Vector operator*(double n, const Vector & a);
		friend std::ostream & operator<<(std::ostream & os, const Vector & v);
};
} // end namespace VECTOR
#endif
// Class definition of VectorMath

#ifndef MATH_H_
#define MATH_H_
#include "vect.h"
namespace MATH
{
class VectorMath
	{
		private:
			VECTOR::Vector max;
			VECTOR::Vector min;
			VECTOR::Vector average;
			VECTOR::Vector tempTotal;
			int numberOfVector;
		public:
			VectorMath() {numberOfVector = 0;}
			~VectorMath() {};
			void calculateMax(const VECTOR::Vector & b) const;
			void calculateMin(const VECTOR::Vector & b) const;
			void firstMin(const VECTOR::Vector & b) const;
			void addToAvg(const VECTOR::Vector & b);
			void calculateAvg() const;
			friend std::ostream & operator<<(std::ostream & os, const VectorMath & v);
	};
} // end of namespace
#endif
// vect.cpp -- methods for the Vector class
#include "stdafx.h"
#include <cmath>
#include "vect.h" // includes <iostream>
using std::sin;
using std::cos;
using std::atan2;
using std::cout;
namespace VECTOR
{
	const double Rad_to_deg = 57.2957795130823;

	// private methods
	// calculates magnitude from x and y
	void Vector::set_mag()
	{
		mag = sqrt(x * x + y * y);
	}

	void Vector::set_ang()
	{
		if (x == 0.0 && y == 0.0)
		ang = 0.0;
		else
		ang = atan2(y, x);
	}

	// set x from polar coordinate
	void Vector::set_x()
	{
		x = mag * cos(ang);
	}

	// set y from polar coordinate
	void Vector::set_y()
	{
		y = mag * sin(ang);
	}

	// public methods

	Vector::Vector() // default constructor
	{
		x = y = mag = ang = 0.0;
		mode = 'r';
	}

	// construct vector from rectangular coordinates if form is r
	// (the default) or else from polar coordinates if form is p
	Vector::Vector(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
			set_mag();
			set_ang();
		}
		else if (form == 'p')
		{
			mag = n1;
			ang = n2 / Rad_to_deg;
			set_x();
			set_y();
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = mag = ang = 0.0;
			mode = 'r';
		}
	}

	// set vector from rectangular coordinates if form is r (the
	// default) or else from polar coordinates if form is p
	void Vector:: set(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
			set_mag();
			set_ang();
		}
		else if (form == 'p')
		{
			mag = n1;
			ang = n2 / Rad_to_deg;
			set_x();
			set_y();
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = mag = ang = 0.0;
			mode = 'r';
		}
	}

	Vector::~Vector() // destructor
	{
	}

	void Vector::polar_mode() // set to polar mode
	{
		mode = 'p';
	}

	void Vector::rect_mode() // set to rectangular mode
	{
		mode = 'r';
	}

	// operator overloading
	// add two Vectors
	Vector Vector::operator+(const Vector & b) const
	{
		return Vector(x + b.x, y + b.y);
	}

	
	// subtract Vector b from a
	Vector Vector::operator-(const Vector & b) const
	{
		return Vector(x - b.x, y - b.y);
	}

	// reverse sign of Vector
	Vector Vector::operator-() const
	{
		return Vector(-x, -y);
	}

	Vector Vector::operator=(const Vector & b) const
	{
		return Vector(b.x, b.y);
	}

	Vector Vector::operator/(const int b) const
	{
		return Vector(x/b, y/b);
	}

	// multiple vector by n
	Vector Vector::operator*(double n) const
	{
		return Vector(n * x, n * y);
	}

	// friend methods

	// multiply n by Vector a
	Vector operator*(double n, const Vector & a)
	{
		return a * n;
	}

	// display rectangular coordinates if mode is r,
	// else display polar coordinates if mode is p
	std::ostream & operator<<(std::ostream & os, const Vector & v)
	{
		if (v.mode == 'r')
			os << "(x,y) = (" << v.x << ", " << v.y << ")";
		else if (v.mode == 'p')
		{
			os << "(m,a) = (" << v.mag << ", "
			<< v.ang * Rad_to_deg << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}
} // end namespace VECTOR
// Contains the code for the average number, highest, and lowest
// for N trials

#include "stdafx.h"
#include "vect.h"
#include "math.h"
#include <iostream>

namespace MATH
{
	void VectorMath::calculateMax(const VECTOR::Vector & b) const
	{
		if (b.magval() > max.magval())
		{
			max = b;
		}
	}

	void VectorMath::calculateMin(const VECTOR::Vector & b) const
	{
		if (b.magval() < max.magval())
		{
			min = b;
		}
	}

	void VectorMath::addToAvg(const VECTOR::Vector & b)
	{
		tempTotal = tempTotal + b;
		numberOfVector++;
	}

	void VectorMath::firstMin(const VECTOR::Vector & b) const
	{
		min = b;
	}

	void VectorMath::calculateAvg() const
	{
		average = (tempTotal/numberOfVector);
	}

	std::ostream & operator<<(std::ostream & os, const VectorMath & v)
	{
		os << "Max is " << v.max.magval() << ", Min is " << v.min.magval() << 
			", and Average is " << v.average.magval();
		return os;
	}
} // end of the namespace
// randwalk.cpp -- using the Vector class
// compile with the vect.cpp file

#include "stdafx.h"
#include <iostream>
#include <fstream> // for I/O operations
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "vect.h"
//#include "math.h"

int main()
{
	using namespace std;
	using VECTOR::Vector;
	srand(time(0)); // seed random-number generator
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	//MATH::VectorMath functions;
	unsigned long steps = 0;
	double target;
	double dstep;
	int numberOfTrials = 0;
	cout << "Please enter the number of trials you would like to run: ";
	cin >> numberOfTrials;
	ofstream outFile;
	outFile.open("output.txt");
	for (int i = 0; i < numberOfTrials; i++)
	{
		cout << "Enter target distance: ";
		cin >> target;
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		cout << "Pass 1" << endl;
		outFile << "Target Distance: " << target << ", Step Size: " << dstep << endl;
		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.set(dstep, direction, 'p');
			outFile << steps << ": " << step << endl;
			result = result + step; // Here is where the issue is
			if (steps == 0)
			{
				//functions.firstMin(result);
			}
			//functions.calculateMax(step);
			//functions.calculateMin(step);
			//functions.addToAvg(step);
			outFile << steps << ": " << result << endl;
			steps++;
		}
		cout << "Pass 2" << endl;
		//functions.calculateAvg();
		//cout << functions;
		cout << "After " << steps << " steps, the subject "
		"has the following location:\n";
		cout << result << endl;
		outFile << "After " << steps << " steps, the subject "
		"has the following location:\n";
		outFile << result << endl;
		result.polar_mode();
		cout << " or\n" << result << endl;
		cout << "Average outward distance per step = "
		<< result.magval()/steps << endl;
		steps = 0;
		result.set(0.0, 0.0);
	}
	outFile.close();
	cout << "Bye!\n";
	return 0;
}

Your "=" operator is just returning the original vector. You need to set the values in the current vector using those from b I think you should use something like:

const Vector &Vector::operator=(const Vector &b)
{
   x = b.x;
   y = b.y;

   return *this;
}

The return *this statement allows you to do things like:

a = b = c;

Nope that did't work, just created more issues.

What kind of issues?

Can you post a main function that does absolutely nothing except present the issue? To be perfectly honest, it's too much effort to paste your code into a project and then divine what inputs are required to make it "not work".

It creates issues in my math.cpp when i am trying to assign max = b; for instance. The other part now works though :)

Never mind, i found the change that I needed to make, just had to take off a const Ty for all of the help :) I am sorry, next time I will try to make my code simpler for example purposes.

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.