简体   繁体   中英

Conversion Constructor in C++

Program below is giving error 'invalid use of incomplete type class Rectangle' and 'forward declaration of class Rectangle'. How to fix this without editing any header file? Isn't there any possible way to do the conversion using constructor only?

include<iostream>
include<math.h>

using namespace std;

class Rectangle;
class Polar
{
    int radius, angle;

public:
    Polar()
    {
        radius = 0;
        angle = 0;
    }

    Polar(Rectangle r)
    {
        radius = sqrt((r.x*r.x) + (r.y*r.y));
        angle = atan(r.y/r.x);
    }

    void getData()
    {
        cout<<"ENTER RADIUS: ";
        cin>>radius;
        cout<<"ENTER ANGLE (in Radians): ";
        cin>>angle;
        angle = angle * (180/3.1415926);
    }

    void showData()
    {
        cout<<"CONVERTED DATA:"<<endl;
        cout<<"\tRADIUS: "<<radius<<"\n\tANGLE: "<<angle;

    }

    friend Rectangle :: Rectangle(Polar P);
};

class Rectangle
{
    int x, y;
public:
    Rectangle()
    {
        x = 0;
        y = 0;
    }

    Rectangle(Polar p)
    {
        x = (p.radius) * cos(p.angle);
        y = (p.radius) * sin(p.angle);
    }

    void getData()
    {
        cout<<"ENTER X: ";
        cin>>x;
        cout<<"ENTER Y: ";
        cin>>y;
    }

    void showData()
    {
        cout<<"CONVERTED DATA:"<<endl;
        cout<<"\tX: "<<x<<"\n\tY: "<<y;
    }

    friend Polar(Rectangle r);


};

You're trying to access the incomplete type Rectangle in your Polar(Rectangle) constructor.

Since the definition of the Rectangle constructor also needs the complete definition of Polar , you'll need to separate the class definition from the constructor definition.

Solution: Put the definitions of your member functions in a .cpp file, as you should be doing, like this:

polar.h:

class Rectangle; // forward declaration to be able to reference Rectangle

class Polar
{
    int radius, angle;
public:
    Polar() : radius(0), angle(0) {} // initializes both members to 0
    Polar(Rectangle r); // don't define this here
    ...
};

polar.cpp:

#include "polar.h"
#include "rectangle.h" // to be able to use Rectangle r

Polar::Polar(Rectangle r) // define Polar(Rectangle)
:   radius(sqrt((r.x*r.x) + (r.y*r.y))),
    angle(atan(r.y/r.x))
{
}

The above initializes radius and angle to what's inside the brackets.

rectangle.h:

class Polar; // forward declaration to be able to reference Polar

class Rectangle
{
    int x, y;
public:
    Rectangle() : x(0), y(0) {} // initializes both x and y to 0
    Rectangle(Polar p); // don't define this here
    ...
};

rectangle.cpp:

#include "rectangle.h"
#include "polar.h" // to be able to use Polar p

Rectangle::Rectangle(Polar p) // define Rectangle(Polar)
:   x((p.radius) * cos(p.angle)),
    y((p.radius) * sin(p.angle))
{
}

I also showed you how to use constructor initialization lists which you should be using in C++ to initialize member variables.

How to fix this without editing any header file.

You can't. The definition of Polar(Rectangle) will have to come after the definition of Rectangle , so that Rectangle is complete where the constructor needs to use it.

Just declare the constructor in the class definition:

Polar(Rectangle r);

and define it elsewhere; either in a source file, or in a header after defining Rectangle (in which case you'll need to mark it inline ).

Personally, I'd tidy this up by splitting it into two headers, one per class, and defining all the members in source files (unless I've proved that they need to be inline for performance reasons). Then the headers will each only need to declare the other class, and only need to be included from the source files that implement or use the classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM