?
Solved

beginner: derived TDialog classes

Posted on 1997-05-17
3
Medium Priority
?
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 100 total points
ID: 1163457
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
ID: 1163458
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
ID: 1163459
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

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