Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto 
Site FAQ 
Sitemap 
Register Now
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 LQrelated cookies.

Introduction to Linux  A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free. 


06132004, 03:28 PM

#1

Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372
Rep:

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 nonmember 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 << " = " << (uv) << 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;
}



06132004, 04:00 PM

#2

Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536
Rep:

Move 'using namespace std' before you include the header file (or add it to the header file). Then it should compile.
When you have a problem like this use
Code:
g++ something.cpp 2>logfile
When you just use >> without a number, you redirect standard output, not standard error. 0, is standard input, 1 standard output and 2 standard error.



06132004, 06:35 PM

#3

Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372
Original Poster
Rep:

thank you, I knw there was a way to do it, but could not find it, thank you :D



Thread Tools 
Search this Thread 


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off



All times are GMT 5. The time now is 05:19 AM.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.

Latest Threads
LQ News

