1
votes

So I have two classes. One has only purely virtual functions. THe other implements those functions and is derived from the first class. I get that i cant instantiate the first class. But when I try to create an object of the second class it fails as well.

This is how my second class looks in general:

class SecondClass  : public FirstClass
{
public:
     SecondClass();
virtual ~SecondClass(void);

void Foo();
void Bar();

}

Implementation:

SecondClass::SecondClass()
{...}
SecondClass::~SecondClass(void)
{...}
void SecondClass::Foo()
{...}
void SecondClass::Bar()
{...}

This how I instantiate it and get the Error:

SecondClass mSecClass;

Where am I going wrong here?

FirstClass.h

class FirstClass
{
public:
  FirstClass(void);
  virtual ~FirstClass(void);

  virtual void Foo() = 0;
  virtual void Bar() = 0;
};
2
Show the interface of FirstClass! Without seen its declarations it is impossible to tell whether SecondClass is abstract or not. - Dietmar Kühl
FirstClass declaration, please :) - Martin James
Sorry guys, I added it =) - tzippy
Copy/paste, don't try to enter it by hand. The FirstClass you post won't compile, because you've tried to name the destructor ~SecondClass. - James Kanze
Tips: use override when you override virtual functions, and read the error message; all half-decent compilers tell you which functions are pure within that class you try to instantiate. - PlasmaHH

2 Answers

0
votes

You need to define the ~FirstClass() destructor and leave out its constructor

class FirstClass
{
public:
  virtual ~FirstClass(void) {} // or use C++11 = default syntax

  virtual void Foo() = 0;
  virtual void Bar() = 0;
};

class SecondClass  : public FirstClass
{
public:
     SecondClass();
virtual ~SecondClass(void);

void Foo();
void Bar();

};

SecondClass::SecondClass() {}
SecondClass::~SecondClass(void) {}
void SecondClass::Foo() {}
void SecondClass::Bar() {}

int main()
{
        SecondClass mSecClass;
}

Live Example.

-2
votes

Define every function you declare, except for pure virtuals(virtual void foo() = 0). try the below code:

#include<iostream>

using namespace std;

class FirstClass
{
public:
    FirstClass()
    {
        //
    }
    virtual ~FirstClass();
    virtual void Foo();
    virtual void Bar();
};
    FirstClass::~FirstClass()
    {
        //
    }
    void FirstClass::Foo()
    {
        //
    }
    void FirstClass::Bar()
    {
        //
    }




class SecondClass  : public FirstClass
{
public:
     SecondClass();
virtual ~SecondClass(void);

void Foo();
void Bar();

};

SecondClass::SecondClass(){
//
}
SecondClass::~SecondClass(void)
{//
}
void SecondClass::Foo()
{//
}
void SecondClass::Bar()
{//
}

int main()
{
    SecondClass name;
    return 0;
}