samedi 12 décembre 2020

Build pattern using header file

I usually see a lot of build pattern implementations in java, and I was trying to do something similar in c++ in which I dont have a lot of knowledge, but I have some code anyhow, Its not working obviously but Its also step forward I guess. Problem is I fail to find builder pattern examples where on uses header files. So I was wondering Is this good way forward, is it done this ways usually, and could I push it to work this way. I am pretty stuck and I have no idea how to fix it.

It results me with following problem

Foo::fooProperties incomplete type is not allowedC/C++(70)

On PlantMonitorProperties plantMonitorProperties; in my header file.

This is my compile error

Build finished with errors(s): ...16:27: error: field ‘plantMonitorProperties’ has incomplete type Foo::FooProperties’ 16 | FooProperties fooProperties; | ^~~~~~~~~~~~~~~~~~~~~~ ...:8:9: note: forward declaration of ‘class Foo::FooProperties’ 8 | class FooProperties;

this is my cpp file

class Foo::FooProperties 
{
    private:
        FooProperties():
            fWithId(0),
            fWithA(false),
            {}

            int fWithId;
            bool fWithA;

            friend class Foo;
            friend class FooBuilder;
};

class  Foo::FooBuilder 
{

public:
    FooBuilder() {}

    FooBuilder& withId(int id) {
        FooProperties.fWithId = id;
        return *this;
    }

    FooBuilder& witha(bool a) {
        FooProperties.fWithA = a;
        return *this;
    }

    Foo build(){
        return Foo(fooProperties);
    }

private:
    FooProperties fooProperties;
};

Foo::Foo(const FooProperties &properties)
 : fooProperties(properties) 
 {}

This is my header file

class Foo
{
    private:
        class FooProperties;

    public:
        class FooBuilder;

    private:
        Foo(const FooProperties& properties);

        FooProperties fooProperties;
};

Aucun commentaire:

Enregistrer un commentaire