2

In my header file, I have declared 2 public member files to be pure virtual functions like so

Header file

class Whatever
{
public:
    virtual bool Update() = 0;
    virtual bool ShouldBeVisible() = 0;
};

Implementation

bool Whatever::Update();

bool Whatever::ShouldBeVisible();

But when I try to compile, I keep an error that says: out-of-line declaration of a member must be a definition for Update and ShouldBeVisible. When I get rid of the semi colons in the implementation I get a different error that says expected ';' after top level declarator and out-of-line declaration of a member must be a definition for Update, and expected function body after function declarator for ShouldBeVisible.

Naq Z
  • 39
  • 1
  • 7

2 Answers2

5

Your second block of text is just more declarations:

bool Whatever::Update();

To have a definition there must be some implementation:

bool Whatever::Update()
{
    return true; // or any other code
}

EDIT:

I see that you want to make them pure virtual functions, meaning that there is no definition. A pure virtual function is like asking for a promise from another class to implement/define these functions. You should use = 0 as you have and simply not define them again except inside another class.

Then you can write classes that implement your whatever interface. Later this will let you use your Whatevers without needing to specifically know which type of whatever you have.

class IsAWhatever : public Whatever
{
public:
    virtual bool Update() override;
    virtual bool ShouldBeVisible() override;
};


bool IsAWhatever::Update()
{
    return true; // or any other code
}

Example of use:

int someFuncThatUsesWhatever(const Whatever& wut); // Defined elsewhere

int main()
{
    IsAWhatever lolwut;

    // This functions accepts a reference to any Whatever so it can
    // take our IsAWhatever class. This is "Runtime Polymorphism" if
    // you want to google it. It is useful for making writing code
    // that might become old and letting it call new code. 
    return someFuncThatUsesWhatever(lolwut);
}
Sqeaky
  • 1,876
  • 3
  • 21
  • 40
  • 1
    So they should not exist in the implementation file? – Naq Z Nov 22 '19 at 02:26
  • @RaunaqZamal If you want to make something pure virtual they shouldn't exist. Just a description of what they look like exists. – Sqeaky Nov 22 '19 at 02:29
  • 3
    IIRC it is legal to provide an implementation of a pure virtual function. Derived classes still need to provide implementations of them, but those implementations might call into the pure implementation. (I don't really like code that does it .. but it is legal) – Michael Anderson Nov 22 '19 at 02:36
  • @MichaelAnderson I don't know if you are correct about legality, but would you agree such behavior is poor form and should generally be avoided as a code smell? – Sqeaky Nov 22 '19 at 02:37
  • 2
    @Sqeaky There is an exception where the definition *must* exist: If the pure virtual method is the destructor, though that is kind of a special case anyway. But I suppose you are correct, that it is code smell. – walnut Nov 22 '19 at 02:42
  • You can read here. It is legal: https://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation and as I said, I don't like it. – Michael Anderson Nov 22 '19 at 06:44
  • @Sqeaky This is done to provide a "default implementation" that can be "chained" by the function in the derived class. The notion of "default implementation" is problematic, as is the semantics of chaining. Virtual functions are subtle tools (much more than non virtual function) and their specification requires great care. In general designing good base classes isn't easy, but clowns waste time criticizing the dangers and abuses of multiple inheritance, and insist on the mythical "is a" (which is poetic gibberish) instead of insisting on sound specifications for virtual functions. – curiousguy Nov 23 '19 at 22:27
0

The compiler wants a function declaration - class Class {void function ();} in the header file and the definition of this function is void Class::function () { int x=2; } compiler wants in cpp file.