Overloading operators p2

2 different compilers, 2 different sets of errors

Page 1 of 1

1 Replies - 759 Views - Last Post: 03 March 2009 - 09:54 AM Rate Topic: -----

#1 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Overloading operators p2

Posted 03 March 2009 - 09:51 AM

Here's the full code for reference:

#include <iostream>
#include <cstdlib>
using namespace std;
class Integer
{
protected:
int a; //available to derived classes
public:
Integer(int n);
Integer();
int geta() const;
void seta(int n);
friend const Integer operator +(const Integer &num1,const Integer &num2);
friend const Integer operator *(const Integer &num1,const Integer &num2);
friend ostream& operator <<(ostream &stream,Integer &n);
friend istream& operator >>(istream &stream,const Integer &n);
};//end Integer class
Integer operator +(Integer &num1,Integer &num2)
{
	return Integer(num1.geta()+num2.geta());
}//end Int +
Integer operator *(Integer &num1,Integer &num2)
{
	return Integer(num1.geta()*num2.geta());
}//end Int *
Integer::Integer()
{
}
Integer::Integer(int n)
{
a=n;
}//end constructor
int Integer::geta() const
{
return a;
}//int geta

istream &operator >>(istream &stream, const Integer &n)
{
 stream >> n.a;
 return stream;
}//end Int >>
ostream &operator <<(ostream &stream, Integer &n)
{
	if (n.a>0) cout<<"+";
	if (n.a<0) cout<<"-";
 stream << n.a;
 return stream;
}//end Int <<

int main()
{
	  Integer intNum1(5), intNum2;
cout << "Please enter an integer number:";
cin >> intNum2; //TA will enter different values to test your program.
cout << "Number1:" << intNum1;
cout << endl;
cout << "Number2:" << intNum2;
cout << endl;
cout << "Result + operator=" << (intNum1+intNum2);
cout << endl;
cout << "Result * operator=" << (intNum1*intNum2);
cout << endl << endl;
	  system("PAUSE");
	  return 0;
}



When I try and compile this in Visual C++ I get 2 weird warnings:

1>cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
1>proj2.cpp
1>f:\cosc 1320red\project2\project2\proj2.cpp(42) : warning C4717: 'operator>>' : recursive on all control paths, function will cause runtime stack overflow

I don't know what is causing either of those. I would assume the 2nd one is in the operator>> function somewhere.

The other weird thing is when I compile it in Dev C++ I'll get 2 errors:
1. Initialization of non-const reference type (error occurs in main when I do int1 + int2).
-"In passing of argument 2 in operator<<(ostream&, Integer&)"
2. Same thing but for when I multiply int1, int2 and output that.

In one compiler I'll get a warning for the operator>> and in the other I'll get an error that seems to be in the operator<< function. So... I'm confused.

Is This A Good Question/Topic? 0
  • +

Replies To: Overloading operators p2

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: Overloading operators p2

Posted 03 March 2009 - 09:54 AM

This isn't directly related to either of your errors, but for your >> overload you have a const Integer reference passed. If you want to be able to modify that Integer's values, it cannot be const. (And vice versa for <<, the Integer should be const so you don't accidentally change values).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1