2

I tried making a simple DI test, where a class car is injected with a container interface.

It gives an error 'incomplete type', see the comment in the code.

What am I doing wrong?

#include <iostream>
#include <string>

class Car;

class IContainer {
public:
    virtual ~IContainer()=default;
};

class Container: public IContainer {
public:
    explicit Container(int i = 1) {
        std::cout << std::to_string(i);
    };

    void makeCar() {
        Car car(this); //Variable has incomplete type 'Car'
    }
};


class Car  {
public:
    explicit Car(IContainer &container): c(container) {
        printf("constructed\n");
    }

private:
    IContainer &c;
};


int main() {

    Container container(5);
    container.makeCar();

    return 0;
}

Solution - here's the working modified code:

#include <iostream>
#include <string>

class Car;

class IContainer {
public:
    virtual ~IContainer()=default;
    virtual void foo(){}
};

class Container: public IContainer {
public:
    explicit Container(int i = 1) {
        std::cout << std::to_string(i) << std::endl;
    };

    void foo() override {
        std::cout << "bar\n";
    }

    void makeCar();

};


class Car  {
public:
    explicit Car(IContainer *container): c(container) {
        printf("Car constructed\n");
        c->foo();
    }

private:
    IContainer *c;
};

void Container::makeCar() {
    //Constructing the car will call a function on the injected container
    Car car(this);
}

int main() {

    Container container(5);
    container.makeCar();

    return 0;
}

6
  • 3
    You're trying to use Car before it has a complete declaration. Put Container::makeCar after Car declaration.
    – Eljay
    Jul 28, 2022 at 21:37
  • @Eljay Make that an answer
    – lorro
    Jul 28, 2022 at 21:39
  • But I'm using forward declaring right? If I move the Car code, then it will not find the container code.
    – Oli
    Jul 28, 2022 at 21:39
  • 1
    @Oli: Eljay didn't tell you to move the whole Container class, but to move the body of makeCar out of the class. You'll need a function prototype/declaration in the class, with no body. And a definition after all classes are defined.
    – Ben Voigt
    Jul 28, 2022 at 21:41
  • 1
    The Car code doesn't need the Container code. It only needs the IContainer code: wandbox.org/permlink/HGX5w4e0QN94morn
    – jabaa
    Jul 28, 2022 at 21:43

1 Answer 1

1

Your C++ compiler reads your C++ program from beginning to the end, compiling as it goes along. At any point your C++ compiler only knows what it's read so far (we'll ignore some specific exceptions that do not matter here).

class Car;

Your C++ compiler now knows that your program has a class named Car. It doesn't know anything at all about this class. Nothing. Zilch. Nada. Zippo.

    void makeCar()
    {
        Car car(this);
    }

Now your C++ compiler needs to generate code that creates such a Car. That's a problem: your C++ compiler doesn't know anything at all about this class. Nothing. Zilch. Nada. Zippo.

The complete class gets declared later, but your C++ compiler doesn't know anything about it.

The solution is simple: just declare the class method here:

    void makeCar();

And then later, after Car is declared, you can define this class method (just before main):

void Container::makeCar() {
    Car car(this); // It works!
}

Everything else remains the same. Now your C++ compiler is omnipotent, all-knowing, and all-powerful. It knows everything now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.