5 Replies - 435 Views - Last Post: 03 July 2013 - 05:21 PM Rate Topic: -----

#1 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Operator Overloading

Posted 03 July 2013 - 11:37 AM

Hi I am defining a class RINT (restricted integer) that behaves like an int except that the only operations allowed are + and - (both unary and binary operators), = for assignment of an int to an RINT, and overloaded stream input >> and output << operators for RINT values. I am not sure on overloading the stream input and output. Any help is appreciated.

h file:

#pragma once

class RINT
{
public:
	RINT(int);
	RINT(void);
	RINT operator--();
	RINT operator+(RINT);
	RINT operator++ () ;    
	RINT operator-- (int);
	RINT operator+= (int);
	RINT operator-= (int);
	RINT operator-(RINT);
   RINT operator=(int);
void print(ostream &out) const {
        // do whatever
    }

	~RINT(void);
private:

	int a;
};


cpp file:

#include "stdafx.h"
#include "RINT.h"
#include <iostream>
using namespace std;

RINT::RINT()
{a = NULL;}

RINT::RINT(int num)
{
	if(a == NULL)
	{
		a = 0;
	}
	else
	{
		a = num;
	}
}



RINT RINT::operator+(RINT num)
{
	RINT ans = *this + num;
	return ans;
}


RINT RINT::operator-(RINT num)
{
	RINT ans = *this - num;
	return ans;
}

RINT RINT::operator=(int num)
{
	RINT ans = num;
	return ans;
}
RINT RINT::operator+= (int num)
{
	RINT ans = num + a;


	return ans;
}

RINT RINT::operator-= (int num)
{
	RINT ans = a - num;

	return ans;
}

ostream &operator<<(ostream &out, const RINT &obj) {
    obj.print(out);
}


RINT::~RINT(void)
{
}


After some reading I started the operator<< function but I am confusing myself and I still get errors in main for std::cout << x where x is a RINT object. It says no operand matches it.

Is This A Good Question/Topic? 0
  • +

Replies To: Operator Overloading

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

Re: Operator Overloading

Posted 03 July 2013 - 11:46 AM

That because you didn't declare the operator overload in your header file.
Was This Post Helpful? 1
  • +
  • -

#3 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Operator Overloading

Posted 03 July 2013 - 02:17 PM

The op<< is all that's needed for this homework, and can just output the RINT data value to the stream object it's passed. So I guess I don't need the print method here.
Shouldnt it work with the correct #includes for iostream, and using statements?
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Operator Overloading

Posted 03 July 2013 - 04:24 PM

Where in the code do you declare and define the overloaded operator<<() for your RINT class?
Was This Post Helpful? 0
  • +
  • -

#5 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Operator Overloading

Posted 03 July 2013 - 05:15 PM

This is what I have now:

#pragma once
#include <iostream>

class RINT
{
public:
	RINT(int);
	RINT(void);
	RINT operator--();
	RINT operator+(RINT);
	RINT operator++ () ;    
	RINT operator-- (int);
	RINT operator+= (int);
	RINT operator-= (int);
	RINT operator-(RINT);
   RINT operator=(int);
 ostream & operator<<(RINT &c);
 istream & operator>>(RINT &c);

	~RINT(void);
private:

	int a;
};


#include "stdafx.h"
#include "RINT.h"
#include <iostream>
using namespace std;
using std::cout;

RINT::RINT()
{a = NULL;}

RINT::RINT(int num)
{
	if(a == NULL)
	{
		a = 0;
	}
	else
	{
		a = num;
	}
}

RINT RINT::operator--()
{
	--a;
	return a;
}

RINT RINT::operator++ () 
{
	a++;
	return a;
}

RINT RINT::operator-- (int num)
{
	RINT ans = a + --(num);  
	
	return ans;
}

RINT RINT::operator+(RINT num)
{
	RINT ans = *this + num;
	return ans;
}


RINT RINT::operator-(RINT num)
{
	RINT ans = *this - num;
	return ans;
}

RINT RINT::operator=(int num)
{
	RINT ans = num;
	return ans;
}
RINT RINT::operator+= (int num)
{
	RINT ans = num + a;


	return ans;
}

RINT RINT::operator-= (int num)
{
	RINT ans = a - num;

	return ans;
}

ostream & operator<<(ostream &is, RINT &c)
{
using std::cout;

cout   << c;
    return is;
}

istream & operator>>(istream &is, RINT &c)
{
using std::cout;

cout   >> c;
    return is;
}

RINT::~RINT(void)
{
}


Still getting errors in main for << >> operands.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1346
  • View blog
  • Posts: 4,636
  • Joined: 19-February 09

Re: Operator Overloading

Posted 03 July 2013 - 05:21 PM

Hi, so your main is something like:

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

using namespace std;

int main()
{
  RINT x;

  x = 24;

  cout << x << endl;

  return 0;
}




If your are compiling the standard way then your RINT.cpp would be added to your project. The RINT.cpp would be compiled separately to the main.cpp, and you would end up having two object files which would be linked to an executable (program).

When the main.cpp is compiled there is no mention of the ostream operator in RINT.h, so the compiler says it cannot find it.

So the answer is to add the head of the function to the header file RINT.h, ie. make a forward declaration.

ostream &operator<<(ostream &out, const RINT &obj);



'

This post has been edited by #define: 03 July 2013 - 05:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1