Page 1 of 1

## 1 Replies - 938 Views - Last Post: 03 March 2009 - 09:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=90334&amp;s=05c81a3e091725f57580ef492b7bbbc9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 agentkirb

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

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

### #2 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07