GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2004, 21:36
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,539
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Question

Error: (function) undeclared -first use of this function


I don't understand why I'm getting these error messages. I have the .h file included in both the main program and the implementation file. The "undeclared" functions are reduce, gcf, and lcd, all having a problem in the main file. Do I need to move these functions to the main program file or am I not referencing them correctly in the header?

Here's the main file (rationalnum.cpp)
CPP / C++ / C Code:
/**************************************************************************
 *   Purpose:  Perform rational number arithmetic, where a rational number is
 *		of the form "a/b", where a and b are integers and b != 0.
 ***************************************************************************/
#include <iostream>
#include <algorithm>
#include "ratnum.h"

using namespace std;

void enterFunction(RatNum& n1, RatNum& n2);

int main ()
{
	cout << "This program performs rational number operations.  A rational number is\n"
		<< "in the form \"a/b\", where a and b are integers and b != 0.\n\n"
		<< "The following operations are available (with examples given):\n"
		<< "1. Addition (3/8 + 1/6 = 13/24)\n"
		<< "2. Subtraction (3/8 - 1/6 = 5/24)\n"
		<< "3. Multiplication (3/8 * 1/6 = 1/16)\n"
		<< "4. Division (3/8 / 1/6 = 9/4)\n"
		<< "5. Inversion (3/8 I = 8/3)\n"
		<< "6. Mixed fraction (8/3 M = 2 + 2/3)\n"
		<< "7. Reduce (6/8 R = 3/4)\n"
		<< "8. Greatest common divisor (6/8 G = 2)\n"
		<< "9. Lowest common denominator (1/6 L 3/8 = 24)\n"
		<< "10. Less than? (1/6 < 3/8 = true)\n"
		<< "11. Less than or equal to? (1/6 <= 3/8 = true)\n"
		<< "12. Greater than? (1/6 > 3/8 = false)\n"
		<< "13. Greater than or equal to? (1/6 >= 3/8 = false)\n"
		<< "14. Equality (3/8 = 9/24 = true)\n"
		<< "15. Linear equation (2/3 X +2 = 4/5) (X = -9/5)\n"
		<< "16. Quit this program\n"
		<< endl;

		int op_choice;
		do
		{
			cout << "Enter the number of one of the operations listed above."
				<< endl;
			RatNum num1, num2;
			cin >> op_choice;

			//choose number operation
			switch (op_choice)
			{
				case 1:	//add
				{
					enterFunction(num1, num2);
					RatNum sum = num1 + num2;
					cout << "\n" << num1 << "+" << num2 << "=" << sum << endl;
				}
				break;

				case 2:	//subtract
				{
					enterFunction(num1, num2);
					RatNum difference = num1 - num2;
					cout << "\n" << num1 << "-" << num2 << "=" << difference << endl;
				}
				break;

				case 3:	//multiply
				{
					enterFunction(num1, num2);
					RatNum product = num1 * num2;
					cout << "\n" << num1 << "*" << num2 << "=" << product << endl;
				}
				break;

				case 4:	//divide
				{
					enterFunction(num1, num2);
					RatNum quotient = num1 / num2;
					cout << "\n" << num1 << "/" << num2 << "=" << quotient << endl;
				}
				break;

				case 5:	//invert
				{
					RatNum invert;
					int top = invert.A(), bottom = invert.B();
					cout << "Enter the number to invert." << endl;
					cin >> invert;
					swap(top, bottom);
					cout << invert;
				}
				break;

				case 6:
				//mixed fraction
				break;

				case 7:	//reduce
				{
					RatNum input;
					cout << "Enter the number to reduce." << endl;
					cin >> input;
					RatNum red = reduce(input);
					cout << red;
				}
				break;

				case 8:	//gcd
				{
					RatNum input;
					cout << "Enter the number to find GCD." << endl;
					cin >> input;
					int gcd = gcf(input.A(), input.B());
					cout << gcd;
				}
				break;

				case 9:	//lcd
				{
					cout << "Enter the 2 numbers to find the LCD, seperated by 'L'" << endl;
					RatNum n1, n2;
					char sep;
					cin >> n1 >> sep >> n2;
					int least = lcd(n1, n2);
					cout << least;
				}
				break;

				case 10:	//less than
				{
					enterFunction(num1, num2);
					bool less = num1 < num2;
					if (less == true)
						cout << "True" << endl;
					else cout << "False" << endl;
				}
				break;

				case 11:	//less than, equal to
				{
					enterFunction(num1, num2);
					bool less_equal = num1 <= num2;
					if (less_equal == true)
						cout << "True" << endl;
					else cout << "False" << endl;
				}
				break;

				case 12:	//greater than
				{
					enterFunction(num1, num2);
					bool great = num1 > num2;
					if (great == true)
						cout << "True" << endl;
					else cout << "False" << endl;
				}
				break;

				case 13:	//greater than, equal to
				{
					enterFunction(num1, num2);
					bool great_equal = num1 >= num2;
					if (great_equal == true)
						cout << "True" << endl;
					else cout << "False" << endl;
				}
				break;

				case 14: 	//equal
				{
					enterFunction(num1, num2);
					bool equal = num1 == num2;
					if (equal == true)
						cout << "True" << endl;
					else cout << "False" << endl;
				}
				break;

				case 15:	//linear equation
				{
					;
				}
				break;

				default:
					cerr << "Incorrect number entered." << endl;
			}
		}while (op_choice != 16);
		return 0;
}

//enterFunction() accepts input of two rational numbers
void enterFunction(RatNum& n1, RatNum& n2)
{
	cout << "Enter the equation you wish to solve.  Example: 2/3 +4/5"
		<< endl;
	char oper;	//type of arithmetic to perform (+, -, *, /)
	cin >> n1 >> oper >> n2;	//input will be in form of 'a/b + c/d'
}

Here's the header file (ratnum.h)
CPP / C++ / C Code:
/**************************************************************************  *RatNum.h 
 *
 *  Purpose:  Declares the class RatNum (RatNumNumber)
****************************************************************************/
#ifndef RATNUM		//compile-once wrapper
#define RATNUM

#include <iostream>
using namespace std;

class RatNum
{
      public:

		RatNum ();												//default-value constructor
		RatNum (int numerator, int denominator);	//explicit-value constructor

		void SetA(int numerator);							//Change value of A
		void SetB(int denominator);						//Change value of B
		int A() const;											//Gives value of A
		int B() const;											//Gives value of B

		//overloaded operators
		RatNum operator +( const RatNum &r ) const;
		RatNum operator -( const RatNum &r ) const;
		RatNum operator *( const RatNum &r ) const;
		RatNum operator /( const RatNum &r ) const;
		bool operator ==( const RatNum &r ) const;
		bool operator >( const RatNum &r ) const;
		bool operator <( const RatNum &r ) const;
		bool operator >=( const RatNum &r ) const;
		bool operator <=( const RatNum &r ) const;

		//rational number functions
		int lcd( RatNum r1, RatNum r2 ) const;
		int gcf( int num1, int num2 ) const;
		RatNum reduce( RatNum &r ) const;

      private:

		int  myA,	//numerator
			myB;	//denominator
};

//-----------Default-value constructor-----------
RatNum::RatNum ()
{
	myA = 0;
	myB = 1;
}

//-----------Explicit-value constructor----------
RatNum::RatNum (int numerator, int denominator)
{
	myA = numerator;
	myB = denominator;
	assert (myB != 0);
	if (myB == 0)
		cerr << "Denominator can not be 0." << endl;
}

//----------Change value of A----------------
inline void RatNum::SetA (int numerator)
{
	myA = numerator;
}

//--------Change value of B-----------
inline void RatNum::SetB (int denominator)
{
	myB = denominator;
}

//-------Return value of A-----------
inline int RatNum::A () const
{
	return myA;
}

//-------Return value of B----------
inline int RatNum::B () const
{
	return myB;
}

//--------Overload output operator--------------
inline ostream & operator<< (ostream & out, RatNum & num)
{
	out << num.A() << "/" << num.B();
	return out;
}

//--------Overload input operator---------------
inline istream & operator>> (istream & in, RatNum & num)
{
	char punc;
	int a, b;
	in >> a >> punc >> b; //input will be in form of 'a/b'
	num.SetA(a);
	num.SetB(b);
	return in;
}

#endif

Here's the implementation file (ratnum.ccp)
CPP / C++ / C Code:
/************************************************************************
 *   Purpose:  Implementation file for RatNum class
 ************************************************************************/
#include <iostream>
#include <algorithm>
#include "ratnum.h"

using namespace std;

//Reduce number to lowest terms
RatNum RatNum::reduce(RatNum &r) const
{
	int gcf_num = gcf(r.myA, r.myB);
	r.myA /= gcf_num;
	r.myB /= gcf_num;
	return r;
}

//Greatest common factor
int RatNum::gcf(int num1, int num2) const
{
	int i;
	if (num2 > num1)
		swap(num1, num2);
	for (i = num1; i > 1; i--)
	{
		if ((num1 % i == 0) && (num2 % i == 0))
		{
			num1 /= i;
			num2 /= i;
		}
	}
	return i;
}

//Least common denominator
int RatNum::lcd(RatNum r1, RatNum r2) const
{
	int num1 = r1.B(), num2 = r2.B();
	int common_factor = gcf(num1, num2);
	int product = num1 * num2;
	if (product < 0)
		product = -product;
	return product/common_factor;
}

//overload '+'
RatNum RatNum::operator +(const RatNum& r) const
{
	RatNum temp;
	temp.myA = myA + r.myA;
	temp.myB = myB + r.myB;
	return reduce(temp);
}

//overload '-'
RatNum RatNum::operator -(const RatNum &r) const
{
	RatNum temp;
	temp.myA = myA - r.myA;
	temp.myB = myB - r.myB;
	return reduce(temp);
}

//overload '*'
RatNum RatNum::operator *(const RatNum &r) const
{
	RatNum temp;
	temp.myA = myA * r.myA;
	temp.myB = myB * r.myB;
	return reduce(temp);
}

//overload '/'
RatNum RatNum::operator /(const RatNum &r) const
{
	RatNum temp;
	temp.myA = myA / r.myA;
	temp.myB = myB / r.myB;
	return reduce(temp);
}

bool RatNum::operator ==(const RatNum& r) const
{
	//'this' refers to the rational number object being executed
	return (this->myA ==r.myA && this->myB == r.myB);
}

bool RatNum::operator <(const RatNum& r) const
{
	//'this' refers to the rational number object being executed
	RatNum tmp_num = *this - r;
	return (tmp_num.myA < 0 || tmp_num.myB < 0);
}

bool RatNum::operator >(const RatNum& r) const
{
	//'this' refers to the rational number object being executed
	RatNum tmp_num = *this -r;
	return (tmp_num.myA > 0 && tmp_num.myB > 0);
}

bool RatNum::operator <=(const RatNum& r) const
{
	//'this' refers to the rational number object being executed
	RatNum tmp_num = *this -r;
	return (tmp_num.myA <= 0 || tmp_num.myB < 0);
}

bool RatNum::operator >=(const RatNum& r) const
{
	//'this' refers to the rational number object being executed
	RatNum tmp_num = *this -r;
	return (tmp_num.myA >= 0 && tmp_num.myB > 0);
}

Thanks for the help.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 28-Oct-2004, 22:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by crystalattice
I don't understand why I'm getting these error messages.
these error messages? What error messages? Ya gotta post 'em!
__________________

Age is unimportant -- except in cheese
  #3  
Old 29-Oct-2004, 05:05
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 890
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
The "undeclared" functions are reduce, gcf, and lcd, all having a problem in the main file. Do I need to move these functions to the main program file or am I not referencing them correctly in the header?

These functions are actually methods belonging to the RatNum class. You need to use them appropriately:

CPP / C++ / C Code:
// RatNum red = reduce(input);  Wrong!
 RatNum red1, red2;
 red1 = red2.reduce(input); // Something like this
                            //I didn't look to what reduce does; modify accordingly

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 29-Oct-2004, 11:09
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,539
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Quote:
Originally Posted by WaltP
these error messages? What error messages? Ya gotta post 'em!
Sorry, I put the error message in the title of my posting; thought it would be easiest. Didn't think to relist it in the actual posting.

For reference, the error was
Code:
(function) undeclared-first use of this function
where (function) is actually 3 functions: reduce, gcf, lcd.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #5  
Old 29-Oct-2004, 17:54
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by crystalattice
Sorry, I put the error message in the title of my posting; thought it would be easiest. Didn't think to relist it in the actual posting.
Oh, OK. I for one ignore titles because 95% of the time they are useless. "Please Help", "What's wrong with my program" and so on. The message is where I expect to see the information to solve the problem.
__________________

Age is unimportant -- except in cheese
  #6  
Old 31-Oct-2004, 11:00
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,539
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Question

Fixed one problem, now have another


I figured out the fix for the original problem (needed to move the default and explict constructors from the header file to the implementation file). Now I have a question about overloading operators.

If I want to overload a '+' sign, it's fairly easy w/ integers.
CPP / C++ / C Code:
//Assume class Number is a two-dimensional vector
Number Number::operator+ (Number param)
{
Number tmp;
tmp.x = x + param.x;
tmp.y = y + param.y;
return tmp;
}

However, since my program deals w/ rational numbers, how do I pass the rational numbers into the overload function? Originally I used the this keyword until I realized that the compiler didn't understand what I wanted by telling me there was no matching function.
Code:
/home/cody/Projects/rationalnum/src/ratnum.cpp:73: no matching function for call to `RatNum::lcd(const RatNum* const, const RatNum&) const'

Here's what I originally had:
CPP / C++ / C Code:
RatNum RatNum::operator +(const RatNum& r) const
{
	//"this" refers to the first rational number, "r" refers to the second
	RatNum temp;
	int lcd_num = lcd(this, r);	//find the common denominator
	int n1 = this->A() * (lcd_num / this->B());	//find the first number's numerator
	int n2 = r.A() * (lcd_num / r.B())	//find the second number's numerator
	temp.SetA(n1 + n2);
	temp.SetB(lcd_num);
	return reduce(temp);
}

The way I interpret the error message is that I can't use this because it's a pointer, not an object. If this is true, then how can I pass the original numbers to the overloaded operator? Is it as easy as adding another argument to the operator function? (The books I've looked at only show one argument).
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #7  
Old 01-Nov-2004, 04:36
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 890
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by LuciWiz
These functions are actually methods belonging to the RatNum class. You need to use them appropriately:

CPP / C++ / C Code:
// RatNum red = reduce(input);  Wrong!
 RatNum red1, red2;
 red1 = red2.reduce(input); // Something like this
                            //I didn't look to what reduce does; modify accordingly

Best regards,
Luci

Sorry about this answer, I didn't look through your entire program (missed/disregarded the overloading stuff). So I suggest you also "disregard" my answer, OK ?

Sorry again,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate 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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to interpret characters as they are being entered? nkhambal C Programming Language 18 14-Feb-2006 10:41
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 19:46
Calling functions within a function spudtheimpaler C Programming Language 5 02-Mar-2004 08:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:06.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.