Solved

beginner: derived TDialog classes

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

691 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