0

I am doing a program to convert feet to meter and vice verse. I using classes and type conversions. both conversion functions( using constructor) use members of other class.So i used forward declaration of class FEET

#include<iostream>
#include<conio.h>
using namespace std;
class FEET;
class MEETER
{
int mtr;
double cm;
public:
MEETER()
{
    mtr=0;
    cm=0;
}
void getdata();
void display();
int get_mtr()
{
    return(mtr);
}
double get_cm()
{
    return(cm);
}
MEETER(FEET f)
{
    int feet=f.get_feet();
    double inch=f.get_inch();
    inch+=feet*12;
    cm=inch*2.54;
    mtr=int(cm)/100;
    cm=cm-(mtr*100);
}
~MEETER()
{
}
};
class FEET
{
int ft;
double in;
public:
FEET()
{
    ft=0;
    in=0;
}
void getdata();
void display();
int get_feet()
{
    return(ft);
}
double get_inch()
{
    return(in);
}
FEET(MEETER f)
{
    int mtr=f.get_mtr();
    double cm=f.get_cm();
    cm+=mtr*100;
    in=cm/2.54;
    ft=int(in)/12;
    cm=cm-(ft*12);
}
~FEET()
{
}
};
void MEETER::getdata()
{
cout<<"\nEnter length in meter and centimeter\n";
cin>>mtr>>cm;
}
void MEETER::display()
{
 cout<<"\n"<<mtr<<"m "<<cm<<"cm\n";
}
void FEET::getdata()
{
 cout<<"\nEnter length in feet and inch\n";
 cin>>ft>>in;
}
void FEET::display()
{
 cout<<"\n"<<ft<<"\""<<in<<"\n";
}
int main()
{
FEET f1,f2;
MEETER m1,m2;
f1.getdata();
m1=f1;
m2.getdata();
f2=m2;
f1.display();
m1.display();
f2.display();
m2.display();
getch();
return(0);
}

But program shows error as follows:

1>------ Build started: Project: Type_Length, Configuration: Debug Win32 ------
1>  source.cpp
1>g:\abhi\type_length\type_length\source.cpp(27): error C2027: use of undefined type 'FEET'
1>          g:\abhi\type_length\type_length\source.cpp(4) : see declaration of 'FEET'
1>g:\abhi\type_length\type_length\source.cpp(27): error C2228: left of '.get_feet' must have class/struct/union
1>g:\abhi\type_length\type_length\source.cpp(28): error C2027: use of undefined type 'FEET'
1>          g:\abhi\type_length\type_length\source.cpp(4) : see declaration of 'FEET'
1>g:\abhi\type_length\type_length\source.cpp(28): error C2228: left of '.get_inch' must have class/struct/union
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Help me to fix it

Winestone
  • 1,500
  • 9
  • 19

3 Answers3

1

So, you have forward declared your class FEET that means compiler knows that FEET is some user defined data type.

But later your are doing somethng like this in your MEETER class

MEETER(FEET f)

i.e you are forcing compiler to predict the size of class FEET which you haven't defined till yet. That's why it's complaining.

To get around this you can use pointer to FEET in your MEETER class as it won't force compiler to know about the size of your class FEET.

MEETER(FEET* f)

Also, prohibit using anything in this function specific to FEET class which mandates compiler to know definition of your class.

ravi
  • 10,994
  • 1
  • 18
  • 36
0

This should answer your question: When can I use a forward declaration?

From there:

What you cannot do with an incomplete type:

  • Define functions or methods using this type

    void f1(X x) {} // compiler error!
    X f2() {} // compiler error!

The error is due to your MEETER(FEET f) { ... } line

Community
  • 1
  • 1
Winestone
  • 1,500
  • 9
  • 19
0

The error occurs because the compiler does not have the definition of class FEET used in the definition of constructor

MEETER(FEET f)
{
    int feet=f.get_feet();
    double inch=f.get_inch();
    inch+=feet*12;
    cm=inch*2.54;
    mtr=int(cm)/100;
    cm=cm-(mtr*100);
}

You should declare the constructor in the class definition of MEETER and define it outside the class after the definition of class FEET

For example

class MEETER
{
    MEETER(FEET f);
    // other members
};

class (FEET
{
    // its members
};

MEETER::MEETER(FEET f)
{
    int feet=f.get_feet();
    double inch=f.get_inch();
    inch+=feet*12;
    cm=inch*2.54;
    mtr=int(cm)/100;
    cm=cm-(mtr*100);
}

The same should be done with the constructor of class FEET

FEET(MEETER f)
{
    int mtr=f.get_mtr();
    double cm=f.get_cm();
    cm+=mtr*100;
    in=cm/2.54;
    ft=int(in)/12;
    cm=cm-(ft*12);
}

That is it should be only declared in class FEET and defined outside the class after the both classes will be defined.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335