LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 06-13-2004, 03:28 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
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;
}
 
Old 06-13-2004, 04:00 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,535

Rep: Reputation: 148Reputation: 148
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.
 
Old 06-13-2004, 06:35 PM   #3
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
thank you, I knw there was a way to do it, but could not find it, thank you :-D
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shut down error log location Psibur Linux - Newbie 1 10-27-2005 03:11 PM
Error:Trash is not valid Location letdoit Slackware 1 05-25-2004 09:05 PM
kdevelop: not jumping to error location when clicking on error. sudheernair Linux - Software 0 05-13-2004 07:41 AM
location error kruger Linux - Newbie 0 10-26-2003 05:36 AM
ERROR: /tmp not large enough for downloading defa0009 Linux - General 5 06-22-2003 09:11 PM


All times are GMT -5. The time now is 01:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration