473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Creating object from the child of an abstract class causes to linkage errors

Can someone explain what am I doing wrong?

Header:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CMyBase {
private:
CMyBase(const CMyBase& obj); /* Disable copy constuctor */
void operator=(const CMyBase& obj); /* Disable assignment operator
*/
protected:
CMyBase();
public:
virtual ~CMyBase()=0;

};

class CMyChild : public CMyBase {
public:
CMyChild();
void close();
~CMyChild();
private:
int temp;
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Source
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "main.h"

CMyChild::CMyChild()
: temp (0)
{

}

CMyChild::~CMyChild() {
close();
}

void CMyChild::close() throw() {
temp = 100;
}
int main(int argc, char **argv)
{
CMyChild child;
child.close();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I get to link errors:

main.obj : error LNK2019: unresolved external symbol "protected:
__thiscall CMyBase::CMyBase(void)" (??0CMyBase@@IAE@XZ) referenced in
function "public: __thiscall CMyChild::CMyChild(void)"
(??0CMyChild@@QAE@XZ)

main.obj : error LNK2019: unresolved external symbol "public: virtual
__thiscall CMyBase::~CMyBase(void)" (??1CMyBase@@UAE@XZ) referenced in
function "public: virtual __thiscall CMyChild::~CMyChild(void)"
(??1CMyChild@@UAE@XZ)

Mar 9 '06 #1
6 1712
You're to implement CMyBase::CMyBase and CMyBase::~CMyBase.

Mar 9 '06 #2
posted:
Can someone explain what am I doing wrong?

Header:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CMyBase {
private:
CMyBase(const CMyBase& obj); /* Disable copy constuctor */
void operator=(const CMyBase& obj); /* Disable assignment operator
*/
Should that not return a reference?
protected:
CMyBase();
No point putting that there. Anyway it should be:

CMyBase() {}
public:
virtual ~CMyBase()=0;

Not sure if you can have a _pure_ virtual constructor.
-Tomás
Mar 9 '06 #3
ro********@gmail.com wrote:
Can someone explain what am I doing wrong?

Header:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CMyBase {
private:
CMyBase(const CMyBase& obj); /* Disable copy constuctor */
void operator=(const CMyBase& obj); /* Disable assignment operator
*/
Usually the assignent op looks like:
T& operator=(const T& rhs)
protected:
CMyBase();
public:
virtual ~CMyBase()=0;

};


The base constructor and destructor will be called before and after the
derived constructor and destructor, respectively, they must be implemented.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 9 '06 #4
Thank you, that was actually the original code that I once had. However
I thought that since the base class is abstract why would I have to
implement its constructor and destructor? That's strange, however it
seems there is no other way to avoid handmade implementation

Mar 9 '06 #5
ro********@gmail.com wrote:
Thank you, that was actually the original code that I once had. However
I thought that since the base class is abstract why would I have to
implement its constructor and destructor? That's strange, however it
seems there is no other way to avoid handmade implementation


An abstract class is a class that has one or more pure virtual
functions. That doesn't mean that it can't do anything at all.

class shape
{
public:
shape(int x0, int y0) : x(x0), y(y0) {}
void draw()
{
do_draw(x, y);
}
virtual ~shape() {}
private:
virtual void do_draw(int, int) = 0;
int x, y;
};

This particular one doesn't do anything interesting in its destructor,
but its constructor is essential.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 9 '06 #6

ro********@gmail.com wrote:
Thank you, that was actually the original code that I once had. However
I thought that since the base class is abstract why would I have to
implement its constructor and destructor?


So you can create and destroy objects of that type. You declared the
constructor and destructor and the compiler needs to use them. You must
have a concrete type that derives from the abstract type, right? When
you create and destroy an object of that concrete type, the base object
of the abstract type is created and destroyed as part of the process.

If a class is abstract, that doesn't mean that no objects of that type
can ever be created. You can create as many objects of abstract type as
you wish. But *only* as base objects within objects of a non-abstract
derived type.

Gavin Deane

Mar 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hal Vaughan | last post by:
I'm creating a sequence of JPanels that will each, in turn, be added to (then removed from) a window (kind of like a wizard). I want these panels to not only extend the JPanel class, but to...
10
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
6
by: yohaas | last post by:
I'm trying to do something, not sure if it's possible or even if it makes sense, but I figured I'd throw it out there. I have a class OrderSummary that contains information about an order. I...
10
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
4
by: Bart Simpson | last post by:
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;...
4
by: Flomo Togba Kwele | last post by:
I am having trouble with the following design. I want an object containing a Parent object and a List of its Child objects. The Child object is abstract, so the List must contain concrete objects...
7
by: Daniel Smedegaard Buus | last post by:
Hello there :) I'm trying to implement some basic DRY MVC-like functionality as a basis to work on. Specifically, I'd like to create a base abstract model class with basic read, write, delete...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.