Solved

beginner: derived TDialog classes

Posted on 1997-05-17
3
470 Views
Last Modified: 2008-03-03
Please excuse the elementary nature of this question, but I'm just starting. I'm learning object construction and inheritance using Borland C++ v5.01. I've encountered a problem with derived classes. The plan is to derive a class from TDialog, which brings up an address book dialog box with a listbox containing a list of names, a data display area which displays the address information for whichever name is selected from the list and some buttons to perform various operations upon the records listed in the dialog's listbox (add a record, delete a record, edit a record, etc.).

The first derived class is constructed as such:

// Address Book dialog box class
class TAddressBookDialog : public TDialog
{
public:
   TAddressBookDialog(TWindow* parent, TResId resId)
    : TDialog(parent, resId),
      TWindow(parent)  { }
};


No problem here. Everything works as expected. The trouble arises when I try to derive another class from TAddressBookDialog (to create an ADD RECORD, DELETE RECORD, ETC. dialog, which uses information from TAddressBookDialog). I am constructing the derived
class as such:

// Delete a record dialog box class
class TDeleteRecordDialog : public TAddressBookDialog
{
public:
   TDeleteRecordDialog(TWindow* parent, TResId resId)
    : TAddressBookDialog(parent, resId),
      TDialog(parent, resId),
      TWindow(parent)  { }
};


The error I receive is: "TDialog is not an unambiguous base class of TDeleteRecordDialog."

When I remove the TDialog reference, it reads as such:

// Delete a record dialog box class
class TDeleteRecordDialog : public TAddressBookDialog
{
public:
   TDeleteRecordDialog(TWindow* parent, TResId resId)
    : TAddressBookDialog(parent, resId),
      TWindow(parent)  { }
};


When written this way, the project compiles, but when I activate the DELETE RECORD dialog, I get the following CPU error:

Thread Stopped
 Fault: access violation at 0x507f24:
 read of address 0xfffffff

Can you tell me what I'm missing, here? Any help (or a point in the proper direction) would be appreciated.

Dave Parks
parks@cfw.com
0
Comment
Question by:MooDave
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 50 total points
Comment Utility
You don't have to initialize the TWindow baseclass in your derived class.

Basically your layout is:


class One : public TSomeClass
{
  One(TWindow *p) : TSomeClass(p) {}
};

class Two : public One
{
  Two(TWindow *p) : One(p) {}
};

In class Two's constructor you only initialise One!! not a baseclass like TSomeClass. The constructor from One initializes TSomeClass. So in your constructor of TDeleteRecordDialog you should only initialise TAddressBookDialog not TDialog or TWindow, that's done by the constructor from TAdressBookDialog. The second codesnippet you wrote skips the constructor of TDialog. The first can't distinguish between the TDialog and TWindow (TDialog is derived from TWindow). In general you would call the constructor of the baseclass that's next up in the hierarchy.

Good luck,
.luc.

0
 

Author Comment

by:MooDave
Comment Utility
Thanks for answering my question. I have, however, tried the suggested solution. Your suggestion would have the second derived class read:

class TDeleteRecordDialog : public TAddressBookDialog
{
public:
   TDeleteRecordDialog(TWindow* parent, TResId resId)
    : TAddressBookDialog(parent, resId)  { }
};


This solution nets me the same CPU error as when I removed the TDialog ancestor alone:

Thread Stopped
 Fault: access violation at 0x507f24:
 read of address 0xfffffff

Any other thoughts? Thanks again for the help.
Dave

0
 
LVL 3

Expert Comment

by:LucHoltkamp
Comment Utility
I've written, compiled  and tested some code according to your class hierarchy, it runs perfectly. The runtime error you get is due to a NULL pointer assignent somewhere. The C++ code that runs:

#include <owl\pch.h>

class TAddressBookDialog : public TDialog
{
public:
  TAddressBookDialog(TWindow* parent, TResId resId)
         : TDialog(parent, resId)  { }
};

class TDeleteRecordDialog : public TAddressBookDialog
{
public:
  TDeleteRecordDialog(TWindow* parent, TResId resId)
   : TAddressBookDialog(parent, resId) { }
};

class TestApp : public TApplication {
  public:
    TestApp() : TApplication() {}

    void InitMainWindow()
    {
      TFrameWindow* fw (new TFrameWindow(0, "Sample ObjectWindows Program"));
      SetMainWindow(fw);
      TDeleteRecordDialog* DelDialog(new TDeleteRecordDialog(fw, "DIALOG_1"));
      DelDialog->Execute();
    }
};

int
OwlMain(int /*argc*/, char* /*argv*/ [])
{
  return TestApp().Run();
}

The applet needs a resource file with a dialog DIALOG_1:

DIALOG_1 DIALOG 0, 0, 240, 120
STYLE DS_MODALFRAME | DS_3DLOOK | DS_CONTEXTHELP | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION ""
FONT 8, "MS Sans Serif"
{
 CONTROL "OK", IDOK, "BUTTON", BS_PUSHBUTTON | BS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 186, 6, 50, 14
 CONTROL "Cancel", IDCANCEL, "BUTTON", BS_PUSHBUTTON | BS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 186, 26, 50, 14
 CONTROL "Help", IDHELP, "BUTTON", BS_PUSHBUTTON | BS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 186, 46, 50, 14
}

Hope this'll help you out, good luck,
.luc.

PS: there's an OWL topic group!

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now