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 23-Apr-2009, 22:48
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

Why is this variable undeclared?


I'm just wondering why I get an error when compiling this code?

rational.h
CPP / C++ / C Code:
#ifndef RATIONAL_H_
#define RATIONAL_H_

#include <iostream>
#include <sstream>

class Rational {

	public:
		Rational(int n = 0, int d = 1);
		int setNum(int n);
		int setDenum(int n);
		friend Rational operator+ (const Rational& r1, const Rational& r2);
		friend Rational operator- (const Rational& r1, const Rational& r2);
		friend Rational operator* (const Rational& r1, const Rational& r2);
		friend Rational operator/ (const Rational& r1, const Rational& r2);
		friend std::ostream& operator<< (std::ostream& out, const Rational& r);
		friend std::istream& operator>> (std::istream& in, Rational& r);
	private:
		int num;
		int den;
};
#endif

rational.cpp
CPP / C++ / C Code:
#include<iostream>
#include "Rational.h"

using namespace std;

Rational::Rational(int n, int d){
	//empty constructor
	
}


Rational operator+ (const Rational& r1, const Rational& r2){
	return r1;
}
	
Rational operator- (const Rational& r1, const Rational& r2){
	return r1;
}
	
Rational operator* (const Rational& r1, const Rational& r2){
	return r1;
}
	
Rational operator/ (const Rational& r1, const Rational& r2){
	return r1;
}


ostream& operator<< (ostream& out, const Rational& r){ //output
	return out;
}

istream& operator>> (istream& in, Rational& r){		//input
	char slash;
	int n, d;
	if(in >> n >> slash >> d){
		num = n;
		den = d;
	}
	return in;
}

Error:
Code:
'num' undeclared (first use this function) 'den' undeclared (first use this function

Not sure why it can't find the num and den that I declared in the header file?

Thanks
  #2  
Old 24-Apr-2009, 07:57
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Why is this variable undeclared?


The friend method operator>>() is not a member method so it is not part of an object. It can, however, access r.num.
  #3  
Old 24-Apr-2009, 22:03
scuzzo scuzzo is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 19
scuzzo is an unknown quantity at this point

Re: Why is this variable undeclared?


I believe your problem is you are bringing in r as an object so you need to use r.num.
  #4  
Old 25-Apr-2009, 07:47
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

Re: Why is this variable undeclared?


Quote:
Originally Posted by scuzzo
I believe your problem is you are bringing in r as an object so you need to use r.num.

This was exactly the problem. Thankyou
 
 

Recent GIDBlogAccepted for Ph.D. program 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
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 12:57
Global variable goes out of scope. Fermat C++ Forum 1 16-Apr-2007 20:41
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 17:43

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

All times are GMT -6. The time now is 16:48.


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