0

In my header file for Class1 I have:

class Class1 : Class2::Callback {

  public:

  //Class2::Callback method
  virtual bool class2Method(int i);

}

in Class1.cxx I have:

bool Class1::class2Method(int i) {
  if (i == 1) return true;
  return false;
}

In another place I have:

IWantAClass2Callback((Class2::Callback)instanceOfClass1);

When I try and compile I get the following error:

MyApp.cxx In constructor 'MyApp()':
MyApp.cxx:55:55: error: 'Class2::Callback is an inaccessible base of Class1'
MyApp.cxx:55:55: error: cannot allocate an object of abstract type 'Class2::Callback'
Class2.h:16:10: note:    because the following virtual functions are pure within 'Class2::Callback'
Class2.h:19:18: note:    virtual bool Class2::Callback::class2Method(int)

What am I doing wrong?

Here's the definition of Class2::Callback as defined in Class2.h :

class Class2
{
public:
  struct Callback {
    virtual bool class2Method(int i) = 0;
  };
}

5 Answers 5

3

You have used private inheritance here. The declaration of Class1 should look like this:

class Class1 : public Class2::Callback { // note the keyword "public" here
public:
    //Class2::Callback method
    virtual bool class2Method(int i);
};
3
  • Then you need to show us more code. Specifically, the definition of Class2::Callback. Jan 18, 2012 at 12:52
  • @TomMedley the error is quite clear: Class2::CallBack is an inaccessible base of Class1 means that at the place of the error the relationship is not, well, accessible, which means that it is not public (could be either private or protected). Go back and check where you added the access specifier... The second error line indicates that you have not overridden the pure virtual function, so you might have other issues (is the definition of Class2::CallBack visible while defining Class1?? Are those the first compiler errors you get?) Jan 18, 2012 at 13:01
  • @TomMedley: Hm, in this example it works. Can you find a significant difference? Jan 18, 2012 at 13:02
1

In c++ the default access specifier for classes is private:

 class Class1 : public Class2::Callback

Should do the trick. That or make it a struct whose default access specifier is public.

1

There're two problems in your code:

  1. As many pointed out, you used private inheritance instead of public one.

  2. You use slicing when converting to Class2::Callback: (Class2::Callback)instanceOfClass1. To use polimorphism you need a pointer or a reference. Assuming that you IWantAClass2Callback has following signature:

    void IWantAClass2Callback(Class2::Callback&);

    (return type doesn't matter), correct call would be:

    IWantAClass2Callback(instanceOfClass1);

0

The Callback must reside in public: area of Class2, is that the case? I mean:

class Class2 {
public:
     class Callback {
         virtual bool class2Method(int) = 0
     };
};
0
0

Do you really have

IWantAClass2Callback((Class2::Callback)instanceOfClass1);

in there? what do you expect it to do? you can cast pointers and references, but no instances.

try changing the signature to

void IWantAClass2Callback(Class2::Callback &instance)

and calling

IWantAClass2Callback((Class2::Callback&)instance)
2
  • Or just IWantAClass2Callback(instance), since the conversion is implicit (once the private inheritance problem is fixed). Explicit conversions should only be used if you want to do something weird, otherwise there's a danger of accidentally doing something weird. Jan 18, 2012 at 15:47
  • In this case, you're right because the Class2::Callback is the interface - thus base class of anything passed. I just wanted to stick to the syntax given in the original question.
    – Coder02
    Jan 19, 2012 at 7:08

Your Answer

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

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