Aug 15, 2008 at 9:24am UTC
i wrote a small code to check if a real number is an integar or not...tried to use classes. Created three files-
----file 1---
#include <cstdlib>
#include <iostream>
#include "RealNumberProp"
int main(int argc, char *argv[])
{
RealNumberProp number;
number.getValue();
number.displayValue;
system("PAUSE");
return EXIT_SUCCESS;
}
file2 ---RealNumberProp.cpp---
#include <cstdlib>
#include <iostream>
#include "RealNumberProp"
int RealNumberProp::upperBound(double value)
{
return ceil(value);
}
int RealNumberProp::lowerbound(double value)
{
return floor(value);
}
bool RealNumberProp::isInteger(double value)
{
if((value==upperBound(value))&&(value==lowerBound(value)))
return true;
else
return false;
}
void RealNumberProp::getValue()
{
cout<<"Enter A Number";
cin>>mRealNumber;
}
void RealNumberProp::displayResult()
{
cout<<"The given number is a"<<isInteger(double mRealNumber)?cout<<"n Integer":cout<<" Non-Integar";
}
file 3-----RealNumberProp.h
#include <cstdlib>
#include <iostream>
#include <math>
class RealNumberProp
{
private:
double mRealNumber(0);
int upperBound(double);
int lowerbound(double);
bool isInteger;
public:
void getValue;
void displayResult;
}
NOW, WHEN I COMPILE THIS, i get an error:- RealNumberProp : No Such fiel or directory...
three of these files are placed under the same directory..what's the problem then...? m using dev c++...
Aug 15, 2008 at 9:34am UTC
Well first off the include must be
#include "RealNumberProp.h"
and then the functions you have in your class definition should be declared as regular prototypes, such as:
1 2 3 4 5 6 7
class RealNumberProp
{
...
public :
void getValue();
void displayResult();
};
And there should be a semicolon at the end of your class definition. The
number.displayValue
, did you mean
number.displayResult()
? You need to specify that the ceil() and floor() functions are part of the std namespace, by using std::ceil() and std::floor() or put a using declaration for them somewhere ahead of time and in scope with the function, such as
using (insert appropriate function here);
or just declare that you're using the whole namespace such as
using namespace std;
. Although the second method is preferable.
Sorry if I didn't cover everything!
Last edited on Aug 15, 2008 at 9:53am UTC
Aug 15, 2008 at 10:39am UTC
ya i made some silly mistakes and now the refined code looks like...
#include <cstdlib>
#include <iostream>
#include "RealNumberProp.h"
int main(int argc, char *argv[])
{
RealNumberProp number;
number.getValue();
number.displayResult();
system("PAUSE");
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#include <math.h>
class RealNumberProp
{
private:
double mRealNumber;
int upperBound(double);
int lowerbound(double);
bool isInteger(double);
public:
void getValue();
void displayResult();
};
#include <cstdlib>
#include <iostream>
#include "RealNumberProp.h"
int RealNumberProp::upperBound(double value)
{
return ceil(value);
}
int RealNumberProp::lowerbound(double value)
{
return floor(value);
}
bool RealNumberProp::isInteger(double value)
{
if((value==upperBound(value))&&(value==lowerBound(value)))
return true;
else
return false;
}
void RealNumberProp::getValue()
{
cout<<"Enter A Number";
cin>>mRealNumber;
}
void RealNumberProp::displayResult()
{
cout<<"The given number is a"<<isInteger(double mRealNumber)?cout<<"n Integer":cout<<" Non-Integar";
}
now i get these linker errors:
have no idea why do the come up..
[Linker error] undefined reference to `RealNumberProp::getValue()'
[Linker error] undefined reference to `RealNumberProp::displayResult()'