views:

218

answers:

5

Hi,

I'm working on a C++ command tool project that depends on a third party architecture called ACE (adaptive communication environment). I'm new to Xcode and this is what I've done to have my command tool project "sees" the ACE library.

  • compile the ACE library so that I have a bunch of dynamic libraries: xxx.dylib
  • add the libraries as a dependency to the target (thru target info -> build )
  • add the directory where I have the header files to the header path build setting

In one of the classes named ACE_Configuration, the header file declare a bunch of function as pure virtual. But in the implementation (cpp) file for the class, the functions are defined.

Now when I subclass from ACE_Configuration and instantiate this subclass in main, when I compile Xcode says I cannot instantiate this subclass because some functions are pure virtual. So in effect, Xcode is only looking at the header file and think ACE_Configuration is an abstract class but in fact it's not. Perhaps I'm not incorporating the ACE library the right way? e.g. I shouldn't use it as a dynamic library and that I need to compile everything together? That can't be i think, I'm not sure what i'm missing. Can someone please help? Thanks!

-Tony

+1  A: 

Your subclass still needs to provide its own implementation of the functions in question, even if they do just call straight through to the superclass implementations. Wikipedia has an example of exactly what you're asking about.

Carl Norum
+3  A: 

If I understand correctly, you're seeing something along the lines of:

// header file
class A
{
public:
    virtual void SomeMethod() = 0;
}

// source file
void A::SomeMethod()
{
   // do stuff
}

And then you're taking a subclass of A like so:

class B : public A
{
    void SomeOtherMethod()
    {
       // do other stuff
    }
    // abstract as we're not defining SomeMethod();
}

As Class A declares SomeMethod pure virtual, B still needs to provide it's own local implementation of that method. The VTABLE for B is not set up to look for SomeMethod in A...

The following class would be concrete:

class C : public A
{
    virtual void SomeMethod()
    {
        // do stuff
    }

    void SomeOtherMethod()
    {
        // do other stuff
    }
}

In other words, class B is abstract because it doesn't define SomeMethod().

Josh
A: 

Thank you Carl and Josh.

I should have mentioned that I do have a local implementation at class B.

it does this:

if( !m_config) return -1;
return m_config->remove_section(key,sub_section,recursive);

but somehow it seem to me xcode doesn't know about the implementation of A. Is it because I'm linking dynamically? I do have the object file when I was building ACE. Do i need to point xcode to it in order for it to know the A is not abstract?

-Tony

Tony
But do you have a local implementation for **ALL** of the pure-virtual methods defined in class A, in class B?It's not enough to define some of the pure-virtual functions (or just your own).
Josh
Remember that B **is abstract** until you provide your own definitions for all of the pure-virtual functions defined by A, even if A has definitions for those functions!
Josh
The reason "xcode doesn't know about the implementation of A" is because A has declared them **pure virtual** which basically means "I provide no implementation of this method to my children". Technically it prevents entries being written into the VTABLE of the object...
Josh
Did that solve your issue or do you need some more help?
Josh
A: 

Hi Josh,

Yes, the local implementation implemented ALL of the pure-virtual methods defined in class A, in class B.

The problem is, in the local implementation, the derived class calls the base class' implementation (the base class cpp file is NOT part of the project) and because even though the base class' pure virtual ARE defined (but in a separate cpp file), xcode doesn't know that the base class has definition for the pure virtuals...

that's why I think I'm not setting up the project correctly or maybe I shouldn't just link the dynamic library but build the third party library within my project. (I would be surprised if I have to do that though, which leaves the only possibility as I'm not setting up the project correctly).

For example, say hypothetically xcode can peak at the third party library's object files, it would know that there are definitions for the pure virtual functions (default implementations)!

sigh.....

Please help!

Tony
Can you post some code? Cut out anything proprietary. I'm wondering if you have the exact method signatures (check for consts, typos, etc.). What is the exact error message XCODE is generating? What are the relevant definitions around the error message?
Josh
A: 

My bad, I found the error. It's because the subclass was written long ago and because the base class has since been updated, one of the parameters has a different type. Because of that in effect, I didn't implement one of the pure virtual functions.

Tony
'Doh! It happens to everyone mate. Now you get to find the next problem, yay!!!
Josh
Could you please 'accept' the answer so that people who have the same problem know what to look for?
Josh