-1

consider two C++ classes:

class cTextbox : public cControl{
public:
    ...
protected:
    void onUpdate(bool* keys);
}

class cControl{
public:
    ...
protected:
    virtual void onUpdate(bool*) = 0;
}

This returns me an error C2504: 'cControl': base class undefined when I define it in the CPP file. Is it possible that I cannot pass a pointer as an argument to a virtual function?

GreySkull
  • 23
  • 4

2 Answers2

3

This has nothing to do with your virtual function, or boolean argument.

At the point that you defined cTextBox, cControl doesn't exist yet, so you can't use it as a base. That's why the error message says the base class is undefined: cControl, the base class, is undefined.

Define cControl first.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Always define the base class before its subclass, because the compiler needs to know the full definition of the base class for inheritance. So if you define your subclass before your base class, C++ would throw an error saying that your base class is undefined.

// base class first
class cControl{
public:
    ...
protected:
    virtual void onUpdate(bool*) = 0;
}

// subclass second
class cTextbox : public cControl{
public:
    ...
protected:
    void onUpdate(bool* keys);
}

It's also good to note that the order of construction in C++ is:

  1. Base member functions.
  2. Base constructor.
  3. Subclass member functions.
  4. Subclass constructor.
Jieyun59
  • 1
  • 3
  • The order of subobject construction doesn't really factor into it. C++ isn't executed top-to-bottom. However, it _is_ (roughly) parsed top-to-bottom. – Lightness Races in Orbit Feb 26 '18 at 01:15
  • Thanks for pointing that out. Still, this doesn't change the fact that the compiler needs the full definition of the base class first for the subclass to inherit from it. And it's good to know the order of subobject construction. – Jieyun59 Feb 26 '18 at 01:37