c++ ostream error, messag rot large to trakc down location of error.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
c++ ostream error, messag rot large to trakc down location of error.
I have a small program for rational numbers, there is an error somewhere, however the compiler output is so long after the rror my terminal does not hold it all and I cannot track down the lien number of even which file the problem is in.... I tried using >> temp.txt at the command line to ty and put the output into a textfile, but it gave me a blank text file.
the .h file and the main function in the .cpp were written by my teacher, I tried removign the portions of my code to do with ostream and istream and still got the message.
here is the code if you can help:
Rational.h
Code:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational {
public:
Rational();
Rational(int);
Rational(int, int);
//big three:
~Rational();
Rational(const Rational&);
Rational& operator=(const Rational&);
//accessors
int getNum() const;
int getDen() const;
//binary operators as member functions
Rational operator+(const Rational& rhs);
Rational operator-(const Rational& rhs);
Rational operator*(const Rational& rhs);
Rational operator/(const Rational& rhs);
//unary operators
Rational& operator++();
Rational& operator++(int);
Rational& operator--();
Rational& operator--(int);
private:
int num;
int den;
//utility functions for internal use only
int gcd();
void reduce();
};
//binary operators as non-member functions
Rational operator+(const Rational& lhs, const Rational& rhs);
Rational operator-(const Rational& lhs, const Rational& rhs);
Rational operator*(const Rational& lhs, const Rational& rhs);
Rational operator/(const Rational& lhs, const Rational& rhs);
ostream& operator<<(ostream& os, const Rational& r);
istream& operator>>(istream& is, Rational& r);
#endif
Rational.cpp
Code:
#include <iostream>
#include "Rational.h"
using namespace std;
void f(Rational x);
int main() {
Rational s, t(2), u(-3, 4), v(16, 160);
cout << "s = " << s << endl;
cout << "t = " << t << endl;
cout << "u = " << u << endl;
cout << "v = " << v << endl;
cout << "prefix increment u " << ++u << endl;
cout << "postfix increment u " << u++ << endl;
cout << "now u is " << u << endl;
cout << u << " + " << v << " = " << (u+v) << endl;
cout << u << " - " << v << " = " << (u-v) << endl;
cout << u << " * " << v << " = " << (u*v) << endl;
cout << u << " / " << v << " = " << (u/v) << endl;
v = u * ( s + t);
cout << "u * ( s + t): " << v << endl;
v = 2 + s;
cout << "2 + s: " << v << endl;
//test copy constructor
f(s);
cout << "return from f. s = " << s << endl;
}
void f(Rational x) {
cout << "in f. x = " << x << cout;
}
Rational::Rational() : num(1), den(1)
{
reduce();
}
Rational::Rational(int I) : num(I), den(1)
{
reduce();
}
Rational::Rational(int N, int D) : num(N), den(D)
{
reduce();
}
Rational::~Rational()
{
//I do not think anything is needed here, no pointers.
}
Rational::Rational(const Rational& R)
{
den = R.den;
num = R.num;
reduce();
}
Rational& Rational::operator=(const Rational& R)
{
den = R.den;
num = R.num;
reduce();
return * this;
}
int Rational::getNum() const
{
return num;
}
int Rational::getDen() const
{
return den;
}
Rational Rational::operator+(const Rational& rhs)
{
den += rhs.den;
num += rhs.num;
reduce();
Rational Out(num,den);
return Out;
}
Rational Rational::operator-(const Rational& rhs)
{
den -= rhs.den;
num -= rhs.num;
reduce();
Rational Out(num,den);
return Out;
}
Rational Rational::operator*(const Rational& rhs)
{
den *= rhs.den;
num *= rhs.num;
reduce();
Rational Out(num,den);
return Out;
}
Rational Rational::operator/(const Rational& rhs)
{
den /= rhs.den;
num /= rhs.num;
reduce();
Rational Out(num,den);
return Out;
}
Rational& Rational::operator++()
{
cout << "NoInt" << endl;
num++;
reduce();
return * this;
}
Rational& Rational::operator++(int)
{
cout << "Int" << endl;
num++;
reduce();
return * this;
}
Rational& Rational::operator--()
{
cout << "NoInt" << endl;
num--;
reduce();
return * this;
}
Rational& Rational::operator--(int)
{
cout << "Int" << endl;
num--;
reduce();
return * this;
}
int Rational::gcd()
{
//unused
}
void Rational::reduce()
{
//I modeled this after the C# code I wrote last quarter.
int counter = 9;
int pass = 1;
while (counter > 1)
{
if (((num % counter) == 0) && ((den%counter) == 0))
{
num /= counter;
den /= counter;
}
counter--;
if ((counter == 1) && (pass == 1))
{
counter = 9;
pass = 2;
}
}
}
Rational operator+(const Rational& lhs, const Rational& rhs)
{
int den = (lhs.getDen() * rhs.getDen());
int num = ((lhs.getNum() * rhs.getDen()) + (rhs.getNum() * lhs.getDen()));
Rational Out(num,den);
return Out;
}
Rational operator-(const Rational& lhs, const Rational& rhs)
{
int den = (lhs.getDen() * rhs.getDen());
int num = ((lhs.getNum() * rhs.getDen()) - (rhs.getNum() * lhs.getDen()));
Rational Out(num,den);
return Out;
}
Rational operator*(const Rational& lhs, const Rational& rhs)
{
Rational Out(lhs.getNum() * rhs.getNum(),lhs.getDen() * rhs.getDen());
return Out;
}
Rational operator/(const Rational& lhs, const Rational& rhs)
{
Rational Out(lhs.getNum() * rhs.getDen(), lhs.getDen() * rhs.getNum());
return Out;
}
ostream& operator<<(ostream& os, const Rational& r)
{
os << r.getNum() << "/" << r.getDen();
return os;
}
istream& operator>>(istream& is, Rational& r)
{
int N, D;
char temp;
is >> N >> temp >> D;
Rational Temp(N,D);
r = Temp;
return is;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.