8 Replies - 336 Views - Last Post: 11 December 2012 - 04:12 AM Rate Topic: -----

#1 yankgrl2006  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 12

Seperate Compilation

Posted 10 December 2012 - 07:01 PM

Hi,
I am trying to separately compile a program in C++ using UNIX and pico editor, however I am constantly getting errors. Here is my code. Any help would be appreciated.

Header File
#ifndef RATIONAL_H;
#define RATIONAL_H;
using namespace std;
class rational {
	friend ostream& operator<<(ostream &, rational &);	
	friend istream& operator>>(istream &, rational &);
public:
	rational();
	rational(int, int);
	bool operator<(rational &);
	// post-condition: if calling obj is smaller, returns true; returns false otherwise
	bool operator>(const rational &);
	// post-condition: if calling obj is greater, returns true; returns false otherwise
private:
	int GCD();
	int num;
	int denom;
};
#endif


C++ File
using namespace std;
#include <iostream>
#include <fstream>	// for declaring ifstream and oftream objects
#include <cstdlib>	// for exit(1).
using namespace std;
class rational {
	friend ostream& operator<<(ostream &, rational &);	
	friend istream& operator>>(istream &, rational &);
public:
	rational();
	rational(int, int);
	bool operator<(rational &);
	// post-condition: if calling obj is smaller, returns true; returns false otherwise
	bool operator>(const rational &);
	// post-condition: if calling obj is greater, returns true; returns false otherwise
private:
	int GCD();
	int num;
	int denom;
};
rational::rational()
{
	num = 0; denom = 1;
}
rational::rational(int n, int d)
{
	if (d == 0)
	{
		cout << "Denominator cannot be zero, program is terminated." << endl;
		exit(1);
	}
	num = n;
	denom = d;
}
int rational::GCD()
{
	int n1 = abs(num), n2 = abs(denom), remainder;	
	remainder = n1 % n2;

	while(remainder != 0)
	{
		n1 = n2;
		n2 = remainder;
		remainder = n1 % n2;
	}
	return n2;
}
bool rational::operator>(const rational &obj)
{
	double value1, value2;	// value1: double equivalent of calling object; value2 double equivlent of parameter obj
	
	value1 = static_cast<double>(num) / static_cast<double>(denom);
	value2 = static_cast<double>(obj.num) / static_cast<double>(obj.denom);

	if (value1 > value2)
		return true;
	else
		return false;
}
bool rational::operator<(rational &obj)
{
	double value1, value2;	// value1: double equivalent of calling object; value2 double equivlent of parameter obj
	
	value1 = static_cast<double>(num) / static_cast<double>(denom);
	value2 = static_cast<double>(obj.num) / static_cast<double>(obj.denom);

	if (value1 < value2)
		return true;
	else
		return false;
}	
ostream& operator<<(ostream &out, rational &obj)	
{								
	int gcd = obj.GCD();
	obj.num /= gcd;
	obj.denom /= gcd;
	out << obj.num << "/" << obj.denom;

	return out;
}
istream& operator>>(istream &in, rational &obj)
{
	in >> obj.num >> obj.denom;
	return in;
}
//**********************************************************************
void selectionSort(rational a[], int number_used);
void swap_values(rational & v1, rational & v2);
int index_of_smallest(rational a[], int start_index, int number_used);

void selectionSort(rational a[], int number_used)
{
	int index_of_next_smallest;

    for (int index = 0; index < number_used - 1; index++)
    {
		index_of_next_smallest = index_of_smallest(a, index, number_used);
        swap_values(a[index], a[index_of_next_smallest]);
	}
}
void swap_values(rational & v1, rational & v2)
{
    rational temp;

    temp = v1;
    v1 = v2;
    v2 = temp;
}
int index_of_smallest(rational a[], int start_index, int number_used)
{
    rational min = a[start_index];
    int index_of_min = start_index;

    for (int index = start_index + 1; index < number_used; index++)
        if (a[index] < min)
        {
            min = a[index];
            index_of_min = index;
        }

    return index_of_min;
}
//**********************************************************************
int main()
{
	const int SIZE = 10;
	rational r[SIZE];
	int n = 0;
	ifstream fin;
	ofstream fout;
	
	for(int i = 0; i < 10; i++)
    {
	cout<<"Please Enter 10 Rational Numbers"<<endl;
	cin>>r[SIZE];
    }
	while(fin >> r[n])	
		n++;
	cout << "\n\nBefore sorting, the list of rationals are: " << endl;
	fout << "\n\nBefore sorting, the list of rationals are: " << endl;
	for (int i = 0; i < n; i++)
	{
		cout << r[i] << "  ";
		fout << r[i] << "  ";
	}
	selectionSort(r, n);
	cout << "\n\nAfter sorting, the list of rationals are: " << endl;
	fout << "\n\nAfter sorting, the list of rationals are: " << endl;
	for (int i = 0; i < n; i++)
	{
		cout << r[i] << "  ";
		fout << r[i] << "  ";
	}
	system("PAUSE");
	cout << endl << endl;
	return 0;
}


Main
using namespace std;
int main()
{
	const int SIZE = 10;
	rational r[SIZE];
	int n = 0;
	ifstream fin;
	ofstream fout;
	
	for(int i = 0; i < 10; i++)
    {
	cout<<"Please Enter 10 Rational Numbers"<<endl;
	cin>>r[SIZE];
    }
	while(fin >> r[n])	
		n++;
	cout << "\n\nBefore sorting, the list of rationals are: " << endl;
	fout << "\n\nBefore sorting, the list of rationals are: " << endl;
	for (int i = 0; i < n; i++)
	{
		cout << r[i] << "  ";
		fout << r[i] << "  ";
	}
	selectionSort(r, n);
	cout << "\n\nAfter sorting, the list of rationals are: " << endl;
	fout << "\n\nAfter sorting, the list of rationals are: " << endl;
	for (int i = 0; i < n; i++)
	{
		cout << r[i] << "  ";
		fout << r[i] << "  ";
	}
	system("PAUSE");
	cout << endl << endl;
	return 0;



Is This A Good Question/Topic? 0
  • +

Replies To: Seperate Compilation

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,563
  • Joined: 09-June 09

Re: Seperate Compilation

Posted 10 December 2012 - 07:15 PM

Quote

however I am constantly getting errors.

Can you post the errors?
Was This Post Helpful? 0
  • +
  • -

#3 yankgrl2006  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 12

Re: Seperate Compilation

Posted 10 December 2012 - 07:19 PM

-bash-3.2$ g++ 5.2.2modified.cpp main.cpp
main.cpp: In function `int main()':
main.cpp:5: error: `rational' undeclared (first use this function)
main.cpp:5: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:5: error: expected `;' before "r"
main.cpp:7: error: `ifstream' undeclared (first use this function)
main.cpp:7: error: expected `;' before "fin"
main.cpp:8: error: `ofstream' undeclared (first use this function)
main.cpp:8: error: expected `;' before "fout"
main.cpp:12: error: `cout' undeclared (first use this function)
main.cpp:12: error: `endl' undeclared (first use this function)
main.cpp:13: error: `cin' undeclared (first use this function)
main.cpp:13: error: `r' undeclared (first use this function)
main.cpp:15: error: `fin' undeclared (first use this function)
main.cpp:18: error: `fout' undeclared (first use this function)
main.cpp:24: error: `selectionSort' undeclared (first use this function)
main.cpp:32: error: `system' undeclared (first use this function)
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Seperate Compilation

Posted 10 December 2012 - 09:10 PM

Well, in your 'Main', you need to include the header files of rational_h so that the compiler will know that there is a class name called rational you will be using.

Next, you need to include the <iostream> and <ifstream> header files in your 'Main'.

Try the following suggestions and see if it is able to resolve your problem.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 yankgrl2006  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 12

Re: Seperate Compilation

Posted 10 December 2012 - 09:30 PM

Unfortunately it is still not working these are the errors I am getting now
-bash-3.2$ cd rationalfinal
-bash-3.2$ ls
5.2.2modified.cpp main.cpp rational.h
-bash-3.2$ g++ main.cpp 5.2.2modified.cpp
main.cpp: In function `int main()':
main.cpp:5: error: `rational' undeclared (first use this function)
main.cpp:5: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:5: error: expected `;' before "r"
main.cpp:7: error: `ifstream' undeclared (first use this function)
main.cpp:7: error: expected `;' before "fin"
main.cpp:8: error: `ofstream' undeclared (first use this function)
main.cpp:8: error: expected `;' before "fout"
main.cpp:12: error: `cout' undeclared (first use this function)
main.cpp:12: error: `endl' undeclared (first use this function)
main.cpp:13: error: `cin' undeclared (first use this function)
main.cpp:13: error: `r' undeclared (first use this function)
main.cpp:15: error: `fin' undeclared (first use this function)
main.cpp:18: error: `fout' undeclared (first use this function)
main.cpp:24: error: `selectionSort' undeclared (first use this function)
main.cpp:32: error: `system' undeclared (first use this function)
In file included from 5.2.2modified.cpp:5:
rational.h:1:19: warning: extra tokens at end of #ifndef directive
rational.h:2:19: warning: ISO C requires whitespace after the macro name
In file included from rational.h:3,
from 5.2.2modified.cpp:5:
rational.h:1:19: warning: extra tokens at end of #ifndef directive
rational.h:20:7: warning: no newline at end of file
In file included from 5.2.2modified.cpp:5:
rational.h:20:7: warning: no newline at end of file
5.2.2modified.cpp:6: error: redefinition of `class rational'
rational.h:5: error: previous definition of `class rational'
-bash-3.2$
Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Seperate Compilation

Posted 10 December 2012 - 09:43 PM

Well, can you please post the updated code that you have for the Main...that will enable us to see where the errors are present.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#7 yankgrl2006  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-November 12

Re: Seperate Compilation

Posted 10 December 2012 - 09:52 PM

Here is the update main code...Thanks so much for your help. I really appreciate it.
using namespace std;
#include <iostream>
#include <ifstream>

int main()
{
	const int SIZE = 10;
	rational r[SIZE];
	int n = 0;
	ifstream fin;
	ofstream fout;
	
	for(int i = 0; i < 10; i++)
    {
	cout<<"Please Enter 10 Rational Numbers"<<endl;
	cin>>r[SIZE];
    }
	while(fin >> r[n])	
		n++;
	cout << "\n\nBefore sorting, the list of rationals are: " << endl;
	fout << "\n\nBefore sorting, the list of rationals are: " << endl;
	for (int i = 0; i < n; i++)
	{
		cout << r[i] << "  ";
		fout << r[i] << "  ";
	}
	selectionSort(r, n);
	cout << "\n\nAfter sorting, the list of rationals are: " << endl;
	fout << "\n\nAfter sorting, the list of rationals are: " << endl;
	for (int i = 0; i < n; i++)
	{
		cout << r[i] << "  ";
		fout << r[i] << "  ";
	}
	system("PAUSE");
	cout << endl << endl;
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Seperate Compilation

Posted 10 December 2012 - 10:06 PM

Ok, you seem to be using fout and fin, but where is the file being opened for the above fin and fout statements to be used?

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Seperate Compilation

Posted 11 December 2012 - 04:12 AM

using namespace std;
#include <iostream>
#include <ifstream>


Put the using namespace std; after the includes. And you're not including the header file with the definition of the rational class.

By the way, convention is to use an upper-case first letter on class names.

If you want to get input from the user, then you use cin.

And the include for ifstream is #include <fstream>
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1