c++ + operator helo

Slapshot136

Divide et impera
Reaction score
471
c++ + operator help

I need some c++ operator help, I have a class "Distance" that contains feet and inches, and I am trying to create a + operator to add them, here is what I have:

Code:
Distance Distance::operator +(Distance &d2) const
{
Distance d;
d.inches = this->inches + d2.inches;
d.feet = this->feet + d2.feet;
return d;
}
and this by itself compiles, but when I try to use it in main like this:
Code:
Distance d1(20), d2(17), d3;
d3 = d1 [COLOR="Red"]+ d2[/COLOR];
it gives me an error when I add the red part, but works fine otherwise:

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Distance::eek:perator int(void)" (??BDistance@@QAEHXZ) referenced in function _main

how can I fix this? (it works fine if I just use the = operator, I defined that like this:
Code:
void Distance::operator =(Distance &d2)
{
this->feet = d2.feet;
this->inches = d2.inches;
}
)
 

Slapshot136

Divide et impera
Reaction score
471
Post the code for the class. Do you have any other overloads of operator+?

only one that takes an int, here's the full class

Code:
#include <iostream>
using namespace std;

					// English Distance class
class Distance
{
private:
	int feet;
	int inches;

	// Normalize adjusts feed and inches if inches >= 12;
	void Normalize();

	// utility function to return Distance in inches
	int sizeInInches() const;

public: // Constructors
	Distance();   // default constructor setting feet = 0 and inches = 0;
	Distance (int initialValueInInches);

	// used for int x = dist1;
	operator int();

	Distance operator + (Distance & d2) const;
	void operator += (Distance & d2);
	void operator += (int value);
	void operator = (Distance & d2);
	void operator = (int value);
	bool operator == (Distance & d2);
	bool operator < (Distance & d2);

	// ******************************
	void getdist();		// get feet and inches from user
	void showdist();	// display feet and inches
};

class ArrDistance
{
	Distance * arr;
	int arrSize;
public:
	// Constructor creates an Array of Distance classes the
	// size specified in size.
	ArrDistance(int size);

	// Make sure you free up your dynamic allocation
	~ArrDistance();

	// This operator should do a sanity check on the index
	Distance & operator [] (int index);

};

Code:
#include "Distance.h"

Distance::Distance()
{
	feet = 0;
	inches = 0;
}

Distance::Distance(int initialValueInInches)
{
	feet = 0;
	inches = initialValueInInches;
}

void Distance::Normalize()
{
	while ((inches / 12) != 0)
	{
		inches -= 12;
		feet++;
	}
}

int Distance::sizeInInches() const
{
	return (inches + (feet * 12));
}

void Distance::showdist()
{
	Normalize();
	cout << "feet: " << feet << "\t inches: " << inches;
}

void Distance::getdist()
{
	// get feet and inches from user
}

// *******************************************
//			Operators
// *******************************************


Distance Distance::operator +(Distance &d2) const
{
	Distance d;
	d.inches = this->inches + d2.inches;
	d.feet = this->feet + d2.feet;
	return d;
}

//++++++++++++++++++++++++++++++++++++++

void Distance::operator +=(Distance &d2)
{
	this->feet += d2.feet;
	this->inches += d2.inches;

}

void Distance::operator +=(int value)
{
	this->inches += value;
}

//++++++++++++++++++++++++++++++++++++++

void Distance::operator =(int value)
{
	this->feet = 0;
	this->inches = value;
}

void Distance::operator =(Distance &d2)
{
	this->feet = d2.feet;
	this->inches = d2.inches;
}

//++++++++++++++++++++++++++++++++++++++

bool Distance::operator ==(Distance &d2)
{
	this->Normalize();
	d2.Normalize();
	return ((this->feet == d2.feet) && (this->inches == d2.inches));
}

bool Distance::operator <(Distance &d2)
{
	this->Normalize();
	d2.Normalize();

	if (this->feet < d2.feet)
		return true;
	else if (this->feet > d2.feet)
		return false;

	// feet are equal, comparing inches

	if (this->inches < d2.inches)
		return true;

	return false;

}
 

Slapshot136

Divide et impera
Reaction score
471
You don't appear to have provided a definition for operator int(), despite declaring it.
it's a work in progress.. does that affect the problem in some way? (commenting it out didn't change anything)

that would be used if I was trying to set an integer equal to a distance, I can't get a distance to be equal to the sum of 2 distances
 
Reaction score
333
it's a work in progress.. does that affect the problem in some way? (commenting it out didn't change anything)

It is relevant because operator int() is the unresolved external from your error message. VC++ is generating code that calls operator int() and the linker is failing because there is no object code associated with operator int().

Anyway, after examining your code further it seems that the type for operator+ is incorrect and that this is causing VC++ to identify "d1 + d2" as integer addition. Try adding const qualifiers to your operators, like this (changes in bold):

Code:
[B]const[/B] Distance Distance::operator +([B]const[/B] Distance &d2) const
void operator =([B]const[/B] Distance & d2)

Making these changes fixed the problem for me in VC++ 2008.

Before continuing you should probably read this page on operator overloading. It is important to make your operators const correct and to give them the correct return types. For example, assignment operators should return mutable references to their left hand operands (lvalues), to allow things like this:

Code:
x = (y = z);
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top