ABCs (abstract base classes) and linkage problems

B

Bart Simpson

I m getting a ton of linkage errors (unresolved externals) when using an
abstract base class. My classes look something like this:

class BaseObject
{
//pure virtuals
virtual void foo() = 0;
virtual int foobar(int, int, double) = 0 ;

virtual void grok();
virtual int doit();
}


class MyClass : public BaseObject
{
//pure virtuals are implemented
void foo() {...}
int foobar(int a, int b, double c){return 42;}

//these are not implemented .. and not called in code
//virtual void grok();
//virtual int doit();

}


Linkage errors (unresolved externals):

BaseObject::BaseObject(void)
void grok()
int doit();


which leads to my questions:

i). Why is the linker looking for the constructor of the ABC BaseObject?
ii). Do I need to implement the constructor of BaseObject ? (this goes
against my understanding of most languages definition of an interface)
iii).Why is the linker looking to resolve the two (non pure) virtuals in
the interface - I dont use it any where in the code - and it is only
there for "future comapatability"

What am I doing wrong ?
 
S

Salt_Peter

I m getting a ton of linkage errors (unresolved externals) when using an
abstract base class. My classes look something like this:

class BaseObject
{
//pure virtuals
virtual void foo() = 0;
virtual int foobar(int, int, double) = 0 ;

virtual void grok();
virtual int doit();

}
; // missing semicolon
class MyClass : public BaseObject
{
//pure virtuals are implemented
void foo() {...}
int foobar(int a, int b, double c){return 42;}

//these are not implemented .. and not called in code
//virtual void grok();
//virtual int doit();

}
; // missing semicolon
Linkage errors (unresolved externals):

BaseObject::BaseObject(void)
void grok()
int doit();

which leads to my questions:

i). Why is the linker looking for the constructor of the ABC BaseObject?

Because even though the base object's ctor is not declared by you, the
compiler will generate one that is non-virtual and implemented
(assuming you declared your class appropriately).
ii). Do I need to implement the constructor of BaseObject ? (this goes
against my understanding of most languages definition of an interface)

No, that depends on what you do with that class. First, you must
declare a class. You didn't.
class Base { }; // is a class
class Base { } // is not
 
B

Bart Simpson

Salt_Peter wrote:

; // missing semicolon
</snip>

If you had bothered to read the title of this post, you would have
realized that these were linkage (not compiler) errors - hence your
nitpicking over missing semicolons, totally misses the point, and does
not add any useful information. The code snipet I posted was a summary
to give a gist/overview of the object heirarchy that illustrated what I
was contending with .... in any case, I managed to resolve the issue
myself (hence the deletion of the post)
 
S

Salt_Peter

Salt_Peter wrote:



</snip>

If you had bothered to read the title of this post, you would have
realized that these were linkage (not compiler) errors - hence your
nitpicking over missing semicolons, totally misses the point, and does
not add any useful information. The code snipet I posted was a summary
to give a gist/overview of the object heirarchy that illustrated what I
was contending with .... in any case, I managed to resolve the issue
myself (hence the deletion of the post)

Please read the FAQ, something that is expected of you _before_
posting here, partcicularly this section:
http://www.parashift.com/c++-faq-lite/how-to-post.html
[5.8] How do I post a question about code that doesn't work correctly?
 
J

James Kanze

I m getting a ton of linkage errors (unresolved externals)
when using an abstract base class. My classes look something
like this:
class BaseObject
{
//pure virtuals
virtual void foo() = 0;
virtual int foobar(int, int, double) = 0 ;
virtual void grok();
virtual int doit();
}
class MyClass : public BaseObject
{
//pure virtuals are implemented
void foo() {...}
int foobar(int a, int b, double c){return 42;}
//these are not implemented .. and not called in code
//virtual void grok();
//virtual int doit();
}
Linkage errors (unresolved externals):
BaseObject::BaseObject(void)
void grok()
int doit();
which leads to my questions:
i). Why is the linker looking for the constructor of the ABC BaseObject?

Is it declared in the base class. The constructor of MyClass
will implicitly call it. If you don't declare it in the base
class, the compiler automatically generates one, which is
inline, so you shouldn't get any linker errors because of it.
If you do declare it, however, it will be called, and so you
must have an implementation.
ii). Do I need to implement the constructor of BaseObject ? (this goes
against my understanding of most languages definition of an interface)

If you declare it, you have to define it. If you're really
defining an interface, typically, you don't declare it, and
there's no problem.
iii).Why is the linker looking to resolve the two (non pure) virtuals in
the interface - I dont use it any where in the code - and it is only
there for "future comapatability"

A function which is declared virtual (but not pure virtual) is
"by definition" used as soon as you instantiate the class, or an
class derived from it. So you need a definition. (The reason
for this is that the compiler will typically want to put the
address of the function in the vtable.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top