This topic is really hard for me to grasp. I tried making a program to test out operator overloading and I can't seem to get it to work.

#ifndef TESTCLASS	
#define TESTCLASS
using namespace std;

class testClass
{
public:
	testClass(double, double);
	friend testClass operator+(testClass& a, testClass& b);
private:
	double num1, num2;
};

testClass::testClass(double numA, double numB)
{
	num1 = numA;
	num2 = numB;
}
testClass operator+(testClass& a, testClass& b)
{	
	return(a.num1 + b.num1, a.num2 + b.num2);
}

#endif

#include <iostream>
#include "testClass.h"
#include <string>
using namespace std;
int main()
{
	testClass obj1(1.1,2.2);
	testClass obj2(2.2,3.3);
	testClass obj3;

	obj3 = obj1 + obj2;

	cout << obj3 << endl;
	return 0;
}

I'm really lost.

Recommended Answers

All 7 Replies

What are you lost about? Are you having compilation issues or getting unexpected results?

One thing that I'm noticing is that you don't have a default constructor for your "testClass" class. You are going to need one to be able to create "obj3".

Also, I see that your arguments for your operator are references, which is inherently dangerous. They need to be const references.

It's also not possible to return more than one value from a function. Which makes this statement: return(a.num1 + b.num1, a.num2 + b.num2); incorrect.

Since obj3 is default constructed, you need a default constructor. The default constructor is only provided for you if you don't already have another constructor defined. Adding default arguments to your two argument constructor will have the same effect though.

Also, you're trying to send obj3 to cout directly, but there's no operator<< defined for testClass.

Finally, and this is the direct issue you're having with operator overloading, operator+ isn't returning a testClass object, it's returning a double (specifically, a.num2 + b.num2 due to the comma operator). Since you're trying to call the constructor directly, you need to specify the class type:

#ifndef TESTCLASS
#define TESTCLASS
#include <ostream>

using namespace std;

class testClass
{
public:
    testClass(double = 0, double = 0);
    friend testClass operator+(testClass& a, testClass& b);
    friend ostream& operator<<(ostream& out, const testClass& obj);
private:
    double num1, num2;
};

testClass::testClass(double numA, double numB)
{
    num1 = numA;
    num2 = numB;
}

testClass operator+(testClass& a, testClass& b)
{
    return testClass(a.num1 + b.num1, a.num2 + b.num2);
}

ostream& operator<<(ostream& out, const testClass& obj)
{
    return out << obj.num1 << ',' << obj.num2;
}

#endif

#include <iostream>
//#include "testClass.h"
#include <string>
using namespace std;
int main()
{
    testClass obj1(1.1,2.2);
    testClass obj2(2.2,3.3);
    testClass obj3;

    obj3 = obj1 + obj2;

    cout << obj3 << endl;
    return 0;
}

Since obj3 is default constructed, you need a default constructor. The default constructor is only provided for you if you don't already have another constructor defined. Adding default arguments to your two argument constructor will have the same effect though.

Also, you're trying to send obj3 to cout directly, but there's no operator<< defined for testClass.

Finally, and this is the direct issue you're having with operator overloading, operator+ isn't returning a testClass object, it's returning a double (specifically, a.num2 + b.num2 due to the comma operator). Since you're trying to call the constructor directly, you need to specify the class type:

#ifndef TESTCLASS
#define TESTCLASS
#include <ostream>

using namespace std;

class testClass
{
public:
    testClass(double = 0, double = 0);
    friend testClass operator+(testClass& a, testClass& b);
    friend ostream& operator<<(ostream& out, const testClass& obj);
private:
    double num1, num2;
};

testClass::testClass(double numA, double numB)
{
    num1 = numA;
    num2 = numB;
}

testClass operator+(testClass& a, testClass& b)
{
    return testClass(a.num1 + b.num1, a.num2 + b.num2);
}

ostream& operator<<(ostream& out, const testClass& obj)
{
    return out << obj.num1 << ',' << obj.num2;
}

#endif

#include <iostream>
//#include "testClass.h"
#include <string>
using namespace std;
int main()
{
    testClass obj1(1.1,2.2);
    testClass obj2(2.2,3.3);
    testClass obj3;

    obj3 = obj1 + obj2;

    cout << obj3 << endl;
    return 0;
}

So whenever I want to cout the results from adding 2 objects I have to overload '<<'? How does the I/O operators work? Will it always start out as "(ostream &something, const classname& obj)"? Also do I always have to pass the values in by reference? I never really understood that and I noticed a lot of the programs in the book uses const ref.

So whenever I want to cout the results from adding 2 objects I have to overload '<<'?

No. If you define a type and want cout << obj; to work (where obj is an object of the new type), there has to be an overloaded operator<< for that type. Otherwise you need to figure out some other way of supporting stream output. Overloading operator<< is for consistency with built-in types when you define a type that needs to support being written to a stream.

How does the I/O operators work?

Magic. :D But seriously, that's a can of worms that you probably don't want to open just yet. Start by learning how to use the feature, then later you can dig down into how it actually works under the hood.

Will it always start out as "(ostream &something, const classname& obj)"?

For the most part, yes.

Also do I always have to pass the values in by reference?

The stream object must be passed by reference because there's no copy constructor. You can do whatever you want with the other parameter, but it's generally wise to pass by reference for performance reasons, and by const reference if you don't intend to modify the state of the object.

Otherwise you need to figure out some other way of supporting stream output.

Other way as in creating a getter function and calling it with obj3.getValue()? Would it work that way?

Other way as in creating a getter function and calling it with obj3.getValue()? Would it work that way?

That's one option, yes.

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.