Click here to Skip to main content
15,879,047 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C++
// operators 140713.cpp : Defines the entry point for the console application.
//

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

using namespace std;

class Minus
{
	private: int numberminus;

	public:
		Minus()
		{
			numberminus = -1;
		}
		void display()
		{
			cout << "Minus " << numberminus << endl;
		}
		operator Plus()  // Error Plus not known
		{
			return Plus();
		}
};

class Plus
{
private: int numberplus;
public:
	Plus()
	{
		numberplus = 1;
	}
	void display()
	{
		cout << "Plus " << numberplus << endl;
	}
	operator Minus()
	{
		return Minus();
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	Minus m, m1;
	Plus p, p1;

	cout << "OUTPUT" << endl;

	m.display();
	p.display();

	m1 = p;
	p1 = m;         // error

	m1.display();
	p1.display();

	cin.get();

	return 0;
}


How do I fix this? The Plus operator in the Minus class doesn't work because the Plus class hasn't been defined yet.

Surely this must be easy to do right?
Posted
Comments
enhzflep 15-Jul-14 15:55pm    
Not quite. The list of valid operators in c++ does not include one called 'Plus'. Basically, you can't define your own operators - you can define how the existing ones work for your class, but you cant create them.

Same problem with your Minus class.

Also, neither of the following are any good - they're 2 entirely separate and unrelated classes - you cant just assign one to the other like this.

m1 = p;
p1 = m;
Sergey Alexandrovich Kryukov 15-Jul-14 18:45pm    
As Rob correctly pointed out in Solution 1, Plus() is a value type conversion operator.
OP was almost there, even mentioned a forward (only it should be a forward definition).
—SA
enhzflep 15-Jul-14 23:16pm    
Thanks SA. :)
Guess I'm suffering a bout of weapons-grade stupidity today..
Sergey Alexandrovich Kryukov 15-Jul-14 23:21pm    
:-)
Ranger49 15-Jul-14 16:23pm    
Thankyou, if I understand this correctly I am trying to do something impossible. The Minus operator does work though. I have this from a book about C++, however in this book there was no Plus operator in the Minus class.

1 solution

The operator Plus() and operator Minus() operators are not arithmetic operators but cast (conversion) operators, so they are fine (enhzflep is wrong here). They each return a newly constructed value of the requested type.

The issue is simply that you're using each of them before they're fully defined.

To solve this.

1. Add the following line before the Minus() class:

C++
class Plus;


This declares that there is a class called Plus that will be defined later.

2. Replace the definition of operator Plus() in class Minus with the following:

C++
operator Plus();


This declares the cast operator without defining it.

3. After the Plus class, add the definition of operator Plus() as follows:

C++
inline Minus::operator Plus() {
    return Plus();
}


This defines the cast operator outside the body of the class, after the needed definitions have been seen.


The key idea here is that a declaration and definition of a function (or variable, or class) can be separated, allowing you
to avoid the need for the two functions to be fully defined. This is common in C++, as source code is processed from the
top down. To use a function, it must have been declared earlier in the source, or (commonly) in a declaration included from a header.

All those #include statements just substitute their contents directly in the source file - so the declarations contained are
all available to the definitions in your code.

Hope this helps.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jul-14 18:43pm    
Looks correct, 5ed.
—SA
enhzflep 15-Jul-14 23:17pm    
It certainly helped me. Thanks Rob. +5
Ranger49 16-Jul-14 9:39am    
Thank you, this does the trick.

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