Howdy guys. I have a program that encompasses creating a Fraction class. In the Fraction class should encompass addition, subtraction, multiplication, and division, and printing out the fraction. For some reason, UNIX hates my cout that I am using in my print. Here are the header and implication files for the class.

Header File

#include<iostream>

class Fraction
{
    public:
        Fraction();
        void setNum(int);
        void setDen(int);
        int getNum();
        int getDen();
        Fraction Add(Fraction);
        Fraction Sub(Fraction);
        Fraction Multiply(Fraction);
        Fraction Divide(Fraction);
        bool IdenticalCompare(Fraction);
        bool ReduceCompare(Fraction);
        void Print();
    private:
        int num;
        int den;
        void Simplify();        
};

Implementation

#include <iostream>
#include "fraction.h"
void Fraction:: Simplify()
{
    for(int x = num*den;x>=num;--x)
    {
        if(den%x==0&&num%x==0)
        {
            num /= x;
            den /= x;
        }
    }
}
Fraction:: Fraction()
{
    num = 1;
    den = 1;
}
//assigning num with the parameter
void Fraction::setNum(int n)
{
    num=n;
}
//assigning den with the parameter
void Fraction::setDen(int d)
{
    den=d;
}
int Fraction::getNum()
{
    return num;
}
int Fraction::getDen()
{
    return den;
}
void Fraction:: Print()
{
    cout<<num<<"/"<<den;
}
Fraction Fraction:: Add(Fraction f2)
{
    Fraction f3;
    f3.num = num*f2.den+f2.num*den;
    f3.den = den*f2.den;
    f3.Simplify();
    return f3;
}
Fraction Fraction:: Sub(Fraction f2)
{
    Fraction f3;
    f3.num = num*f2.den - f2.num*den;
    f3.den = den*f2.den;
    f3.Simplify();
    return f3;
}
Fraction Fraction:: Multiply(Fraction f2)
{
    Fraction f3;
    f3.num = num*f2.num;
    f3.den = den*f2.den;
    f3.Simplify();
    return f3;
}
Fraction Fraction:: Divide(Fraction f2)
{
    Fraction f3;
    f3.num = num*f2.den;
    f3.den = f2.num*den;
    f3.Simplify();
    return f3;
}
bool Fraction:: IdenticalCompare(Fraction f2)
{
    if(num==f2.num&&den==f2.den)
        return true;
    else
        return false;
}
bool Fraction:: ReduceCompare(Fraction f2)
{
    Simplify();
    f2.Simplify();
    if(num==f2.num&&den==f2.den)
        return true;
    else 
        return false;
}

The error I am getting is:

error: 'cout' undeclared (first use this function)

I am also using UNIX if this is any useful information. If there is any other information needed, please let me know.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Just a guess but I noticed you are missing:

using namespace std

Don't know if that is used in UNIX but I've done this many times.

Yes, I tried added that, but it didn't seem to change anything. Should I add it to both the header and implementation?

I am now getting this error after adding "using namespace std;" to both the header and implementation:

Undefined first referenced
symbol in file
main
3/crt1.o
1d: fatal: Symbol referencing errors. No output written to a.out
collect2: 1d returned 1 exit status

I am now getting this error after adding "using namespace std;" to both the header and implementation:

Undefined first referenced
symbol in file
main
3/crt1.o
1d: fatal: Symbol referencing errors. No output written to a.out
collect2: 1d returned 1 exit status

Post the updated code please. Last time you made a slight error on the code tags.

[code]

// paste code here

[/code]

Hit "Preview Post" before you submit and it'll give you a chance to correct code tag errors and anything else.

Fix your code tags again, also is this your whole program? I tried it with using namespace std; and it did not give the cout error but a linking error with a library function.

Header File

#include<iostream>
using namespace std;
class Fraction
{
	public:
		Fraction();
		void setNum(int);
		void setDen(int);
		int getNum();
		int getDen();
		Fraction Add(Fraction);
		Fraction Sub(Fraction);
		Fraction Multiply(Fraction);
		Fraction Divide(Fraction);
		bool IdenticalCompare(Fraction);
		bool ReduceCompare(Fraction);
		void Print();
	private:
		int num;
		int den;
		void Simplify();		
};

Implementation File

#include "fraction.h"
#include <iostream>
using namespace std;

void Fraction::Simplify()
{
	for(int x = num*den;x>=num;--x)
	{
		if(den%x==0&&num%x==0)
		{
			num /= x;
			den /= x;
		}
	}
}
Fraction::Fraction()
{
	num = 1;
	den = 1;
}
//assigning num with the parameter
void Fraction::setNum(int n)
{
	num=n;
}
//assigning den with the parameter
void Fraction::setDen(int d)
{
	den=d;
}
int Fraction::getNum()
{
	return num;
}
int Fraction::getDen()
{
	return den;
}
void Fraction::Print()
{
	cout<<num<<"/"<<den;
}
Fraction Fraction::Add(Fraction f2)
{
	Fraction f3;
	f3.num = num*f2.den+f2.num*den;
	f3.den = den*f2.den;
	f3.Simplify();
	return f3;
}
Fraction Fraction::Sub(Fraction f2)
{
	Fraction f3;
	f3.num = num*f2.den - f2.num*den;
	f3.den = den*f2.den;
	f3.Simplify();
	return f3;
}
Fraction Fraction::Multiply(Fraction f2)
{
	Fraction f3;
	f3.num = num*f2.num;
	f3.den = den*f2.den;
	f3.Simplify();
	return f3;
}
Fraction Fraction::Divide(Fraction f2)
{
	Fraction f3;
	f3.num = num*f2.den;
	f3.den = f2.num*den;
	f3.Simplify();
	return f3;
}
bool Fraction::IdenticalCompare(Fraction f2)
{
	if(num==f2.num&&den==f2.den)
		return true;
	else
		return false;
}
bool Fraction::ReduceCompare(Fraction f2)
{
	Simplify();
	f2.Simplify();
	if(num==f2.num&&den==f2.den)
		return true;
	else 
		return false;
}

Test File (this file is to test to make sure all of the functions are working properly)

#include "fraction.h"
#include<iostream>
using namespace std;
int main()
{
	Fraction f1, f2, f3;
	f1.setNum(1);
	f1.setDen(2);
	f2.setNum(2);
	f2.setDen(3);
	f3 = f1.Add(f2);
	f3.Print();
	cout<<endl;
	f3 = f1.Sub(f2);
	f3.Print();
	cout<<endl;
	f3 = f1.Multiply(f2);
	f3.Print();
	cout<<endl;
	f3 = f1.Divide(f2);
	f3.Print();
	return 0;
}
This article has been dead for over six months. Start a new discussion instead.