I'm writing a program that uses two classes. One class
RoomDimentions
uses the other class
FeetInches
as a member variable. I wrote the class
FeetInches
and tested and it worked fine. Then I wrote the header file for
RoomDimentions
and added it to the project. I put the
#include "RoomDimentions"
at the top of my main .cpp file and then tried to compile to see if there were any syntax errors in the
RoomDimentions.h
file. I got errors stating that the
FeetInches
class used in my main .cpp file were undefined. If I remove the
#include "RoomDimentions"
from the main.cpp file, the program compiles just fine. Any ideas on why the program wont compile just from adding a header file for a class that isn't actually used in the program yet? Or why adding that file some how makes it so the compiler dosen't recognize the
FeetInches
class? (I'm using VS Express 2012 if that helps. I have tried writing the implimentation .cpp file and adding it but I need to be able to test the code as I go and I cant do that till I get past this. I totally appreciate any help!
Here are the two class header files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
//Header file for FeetInches class
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
#ifndef FEETINCHES_H
class FeetInches
{
private:
int feet; // To hold a number of feet
int inches; // To hold a number of inches
void simplify(); // Defined in FeetInches.cpp
public:
// Constructor
FeetInches();
FeetInches(int, int);
//Copy Constructor
FeetInches(FeetInches &);
// Mutator functions
void setFeet(int);
void setInches(int);
// Accessor functions
int getFeet() const;
int getInches() const;
//Overloaded opperators
bool operator <= (const FeetInches &);
bool operator >= (const FeetInches &);
bool operator != (const FeetInches &);
//Multipy Function
FeetInches multiply(FeetInches length);
};
#endif
//Header file for RoomDimention class
#include "stdafx.h"
#include "FeetInches.h"
#ifndef ROOMDIMENTION_H
class RoomDimention
{
private:
FeetInches length;
FeetInches width;
public:
//Constructors
RoomDimention();
RoomDimention(FeetInches &, FeetInches &);
//Accessors
FeetInches getLength();
FeetInches getWidth();
};
#endif
|
Here is the program I was running to test the
FeetInches
without the
#include "RoomDimentions"
at the top of my main .cpp file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include "stdafx.h"
#include "FeetInches.h"
int main()
{
int ft,in;
cout<< "Enter measurment A in feet and inches:"<<endl;
cin>>ft>>in;
FeetInches m1(ft, in);
cout<< "Enter measurment B in feet and inches:"<<endl;
cin>>ft>>in;
FeetInches m2(ft, in);
if (m1 <= m2)
{
cout << "A is less than or equal to B.\n";
}
if (m1 >= m2)
{
cout << "A is greater than or equal to B.\n";
}
if (m1 != m2)
{
cout << "A is not equal to B.\n";
}
cout<<endl;
FeetInches m3 = m2;
cout<<m3.getFeet()<<"ft "<<m3.getInches()<<"in "<<endl;
cout <<"Area of A x B is : "<<endl;
FeetInches Area = m1.multiply(m2);
cout<<Area.getFeet()<<"ft "<<Area.getInches()<<"in "<<endl;
return 0;
}
|