Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Constructor of struct calling the member function of another class declared as a pointer

I have the following code:

class Cohomology;

struct EMField
{
     std::unique_ptr<Cohomology> coh;
     std::array<DIM> data;

     EMField() {coh -> initializeField(*this);};
}

class Cohomology
{
     private:
        // private members
     public:
        Cohomology(PList params)
        {
             // Constructor of the class
        }

        void initializeField(EMField& field)
        {
             field.coh.reset(this);
             // other methods to initialize field.data using the private members
        }
}

In this answer it is explained that calling a method of an incomplete type is not possible, nor dereferencing the pointer.

In fact, when I try to compile it I get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

warning: invalid use of incomplete type ‘class Cohomology‘

note: forward declaration of ‘class Cohomology‘

My question is: How can I delegate the construction of EMField to the Cohomology class if I cannot use a member of std::unique_ptr<Cohomology> coh ?

>Solution :

Just move the definition of EMField::EMField() until after both classes have been defined.

class Cohomology;

struct EMField
{
     std::unique_ptr<Cohomology> coh;
     std::array<DIM> data;

     EMField();
};

class Cohomology
{
     private:
        // private members
     public:
        Cohomology(PList params)
        {
             // Constructor of the class
        }

        void initializeField(EMField& field)
        {
             field.coh.reset(this);
             // other methods to initialize field.data using the private members
        }
};

inline EMField::EMField()
{
    coh -> initializeField(*this);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading