vendredi 1 avril 2022

C++: Explicitly initialize base class

I'm working through a tutorial in C++ and am running into an Undefined symbols for architecture x86_64 error. The way my classes are structured looks like this:

class v_factory {
    // A pure abstract class
public:
    virtual ~v_factory() = 0;

    // a bunch of pure virtual functions

protected:
    // if i put this here, I don't get issue #1 showing up on my compiler
    v_factory() = default;
};

class use_factory : public v_factory {
public:
    // issue #1 will come up here if i don't have that protected default constructor for v_factory **ISSUE #1**
    explicit use_factory(Queue<char> &queue) : q(queue) {}

    // issue #2: not sure what to actually put here, left it as default for now.
    ~use_factory() override = default;

    // overriding a bunch of virtual functions from v_factory here.

private: 
    Queue<char> &q;
};

Issue #1: Constructor for 'use_factory' must explicitly initialize the base class 'v_factory' which does not have a default constructor Not sure how to go about this, if I add the default protected constructor, it goes away but I was looking through a book and I didn't see that a constructor was needed since it is a pure abstract class (since all functions are virtual). Maybe I misunderstood. Not sure what the proper fix would be for this.

Issue #2:

Undefined symbols for architecture x86_64:
  "v_factory::~v_factory()", referenced from:
      use_factory::~use_factory() in Main.cpp.o
ld: symbol(s) not found for architecture x86_64

I'm not sure how to approach this one either. The destructor on v_factory is virtual so doesn't it rely on use_factory to have one? How should I approach this?

EDIT for Issue #2: I believe this may have been resolved when reviewing a Geeksforgeeks page, essentially I believe if I define a destructor for both, I should be good to go.

Sorry for the lengthy question, any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire