Solved

beginner: derived TDialog classes

Posted on 1997-05-17
3
471 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

776 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