A problem with polymorphism

Discussion in 'C++' started by maxgross@gmx.de, Jan 19, 2007.

  1. Guest

    Hi everyone,

    ive got a problem with polymorphism. When i compile the code an error
    occures:

    error: 'class QtElement' has no member named 'DisplayQtElement'

    I asked my friends and nobody could help me, all said it has to work.
    I work with wxWidgets 2.6.3 under Linux.

    Structure of classes:
    wxTreeItemData->QtElement->StructureElement->Assessment
    wxTreeItemData->QtElement->StructureElement->Section
    wxTreeItemData->QtElement->QuestionElement->MultipleChoice
    wxTreeItemData->QtElement->QuestionElement->MultipleResponse

    QtElement, StructureElement, QuestionElement are abstract classes.

    Here the code:
    ---------------------------------------------------------------------------------------------
    #ifndef QT_ELEMENT_H_
    #define QT_ELEMENT_H_
    #include <wx/treectrl.h>

    class QtElement: public wxTreeItemData
    {
    protected:
    QtElement(NativeString title) :title(title) {}

    NativeString title;

    public:
    virtual void DisplayQtElement() = 0;

    virtual NativeString GetTitle() { return title; }

    virtual ~QtElement() {}
    };
    #endif /*QT_ELEMENT_H_*/
    ---------------------------------------------------------------------------------------------
    #ifndef QUESTION_ELEMENT_H_
    #define QUESTION_ELEMENT_H_
    #include "qt_element.h"

    class QuestionElement : public QtElement
    {
    protected:
    QuestionElement(NativeString title, NativeString qText)
    : QtElement(title), questionText(qText) {}

    NativeString questionText;
    public:
    virtual ~QuestionElement() {}

    virtual NativeString GetQuestionText() { return questionText; }
    };
    #endif /*QUESTION_ELEMENT_H_*/
    ---------------------------------------------------------------------------------------------
    #ifndef MULTIPLE_RESPONSE_H_
    #define MULTIPLE_RESPONSE_H_
    #include "question_element.h"
    class MultipleResponse: public QuestionElement
    {
    public:
    MultipleResponse(NativeString title, NativeString qText)
    : QuestionElement("MultipleResponse", title, qText) {}

    virtual ~MultipleResponse() {}

    virtual void DisplayQtElement();

    private:
    };

    inline void
    MultipleResponse::DisplayQtElement()
    {
    // display element
    }
    #endif /*MULTIPLE_RESPONSE_H_*/
    ---------------------------------------------------------------------------------------------
    Usage:
    ---------------------------------------------------------------------------------------------
    ....
    wxTreeCtrl qtTree = new wxTreeCtrl(this, ID_TREE_CTRL,
    wxDefaultPosition, wxSize(100,300),wxTR_SINGLE | wxTR_HIDE_ROOT |
    wxTR_FULL_ROW_HIGHLIGHT | wxTR_HAS_BUTTONS);
    QtElement *qe = new MultipleChoice(title,q_text);
    qtTree->AppendItem(parent, title, -1, -1,
    dynamic_cast<wxTreeItemData*>(qe));
    ....

    QtElement *qt =
    dynamic_cast<QtElement*>(qtTree->GetItemData(selection));
    // if i cast to QuestionElement, StructureElement or the conrete
    classes then it to works

    qt->DisplayQtElement(); // error
    qt->GetTitle(); // is working
    ---------------------------------------------------------------------------------------------
    I have to derive QtElement from wxTreeItemData so that i can add a
    QtElement to the wxTreeCtrl.

    Perhaps someone can help me, thanks.
    Max
    , Jan 19, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > Hi everyone,
    >
    > ive got a problem with polymorphism. When i compile the code an error
    > occures:
    >
    > error: 'class QtElement' has no member named 'DisplayQtElement'
    >
    > I asked my friends and nobody could help me, all said it has to work.
    > I work with wxWidgets 2.6.3 under Linux.
    >
    > Structure of classes:
    > wxTreeItemData->QtElement->StructureElement->Assessment
    > wxTreeItemData->QtElement->StructureElement->Section
    > wxTreeItemData->QtElement->QuestionElement->MultipleChoice
    > wxTreeItemData->QtElement->QuestionElement->MultipleResponse
    >
    > QtElement, StructureElement, QuestionElement are abstract classes.
    >
    > Here the code:
    > ---------------------------------------------------------------------------------------------
    > #ifndef QT_ELEMENT_H_
    > #define QT_ELEMENT_H_
    > #include <wx/treectrl.h>
    >
    > class QtElement: public wxTreeItemData
    > {
    > protected:
    > QtElement(NativeString title) :title(title) {}
    >
    > NativeString title;
    >
    > public:
    > virtual void DisplayQtElement() = 0;
    >
    > virtual NativeString GetTitle() { return title; }
    >
    > virtual ~QtElement() {}
    > };
    > #endif /*QT_ELEMENT_H_*/
    > ---------------------------------------------------------------------------------------------
    > #ifndef QUESTION_ELEMENT_H_
    > #define QUESTION_ELEMENT_H_
    > #include "qt_element.h"
    >
    > class QuestionElement : public QtElement
    > {
    > protected:
    > QuestionElement(NativeString title, NativeString qText)
    > : QtElement(title), questionText(qText) {}
    >
    > NativeString questionText;
    > public:
    > virtual ~QuestionElement() {}
    >
    > virtual NativeString GetQuestionText() { return questionText; }
    > };
    > #endif /*QUESTION_ELEMENT_H_*/
    > ---------------------------------------------------------------------------------------------
    > #ifndef MULTIPLE_RESPONSE_H_
    > #define MULTIPLE_RESPONSE_H_
    > #include "question_element.h"
    > class MultipleResponse: public QuestionElement
    > {
    > public:
    > MultipleResponse(NativeString title, NativeString qText)
    > : QuestionElement("MultipleResponse", title, qText) {}
    >
    > virtual ~MultipleResponse() {}
    >
    > virtual void DisplayQtElement();
    >
    > private:
    > };
    >
    > inline void
    > MultipleResponse::DisplayQtElement()
    > {
    > // display element
    > }
    > #endif /*MULTIPLE_RESPONSE_H_*/
    > ---------------------------------------------------------------------------------------------
    > Usage:
    > ---------------------------------------------------------------------------------------------
    > ...
    > wxTreeCtrl qtTree = new wxTreeCtrl(this, ID_TREE_CTRL,
    > wxDefaultPosition, wxSize(100,300),wxTR_SINGLE | wxTR_HIDE_ROOT |
    > wxTR_FULL_ROW_HIGHLIGHT | wxTR_HAS_BUTTONS);
    > QtElement *qe = new MultipleChoice(title,q_text);
    > qtTree->AppendItem(parent, title, -1, -1,
    > dynamic_cast<wxTreeItemData*>(qe));
    > ...
    >
    > QtElement *qt =
    > dynamic_cast<QtElement*>(qtTree->GetItemData(selection));
    > // if i cast to QuestionElement, StructureElement or the conrete
    > classes then it to works


    Of course. When you dynamic_cast it and assign it, the pointer is loosing
    the polymorphic nature. You are saying, "Here is a pointer pointing to a
    QtElement" and assigning it to qt. As far as qt, and your program, knows,
    it IS a QtElement pointer. And the error is correct, QtElement has no
    DisplayQtElement, because it's declared pure virtual.

    > qt->DisplayQtElement(); // error
    > qt->GetTitle(); // is working
    > ---------------------------------------------------------------------------------------------
    > I have to derive QtElement from wxTreeItemData so that i can add a
    > QtElement to the wxTreeCtrl.
    >
    > Perhaps someone can help me, thanks.
    > Max
    >
    Jim Langston, Jan 19, 2007
    #2
    1. Advertising

  3. kwikius Guest

    wrote:
    *qt =
    > dynamic_cast<QtElement*>(qtTree->GetItemData(selection));
    > // if i cast to QuestionElement, StructureElement or the conrete
    > classes then it to works
    >
    > qt->DisplayQtElement(); // error
    > qt->GetTitle(); // is working
    > ---------------------------------------------------------------------------------------------
    > I have to derive QtElement from wxTreeItemData so that i can add a
    > QtElement to the wxTreeCtrl.
    >
    > Perhaps someone can help me, thanks.
    > Max



    AFAICS it should work, so it looks to me like a bug in the compiler.
    Only thing I can suggest is providing some
    body in the base class which either asserts or throws an exception if
    actually invoked. (Based on the fact that GetTitle works). You could
    make it conditional on the compiler version. The protected ctor should
    prevent instantiation of QtElements.

    regards
    Andy Little
    kwikius, Jan 19, 2007
    #3
  4. kwikius Guest

    Jim Langston wrote:
    > <> wrote in message
    > > QtElement *qt =
    > > dynamic_cast<QtElement*>(qtTree->GetItemData(selection));
    > > // if i cast to QuestionElement, StructureElement or the conrete
    > > classes then it to works

    >
    > Of course. When you dynamic_cast it and assign it, the pointer is loosing
    > the polymorphic nature. You are saying, "Here is a pointer pointing to a
    > QtElement" and assigning it to qt. As far as qt, and your program, knows,
    > it IS a QtElement pointer. And the error is correct, QtElement has no
    > DisplayQtElement, because it's declared pure virtual.


    If that were the case (which it isnt) there would be no point in ABC's:
    (IOW the following wouldnt work)

    #include <iostream>

    struct base{
    virtual void f()const = 0;
    virtual ~base(){}
    };

    struct derived : base{
    void f()const {std::cout << "Hello\n";}
    };

    int main()
    {
    derived d;

    base * b= 0;

    // dynamic cast use for example only
    b= dynamic_cast<base*>(&d);

    b->f();
    }
    kwikius, Jan 19, 2007
    #4
  5. Guest

    Hi,

    first thanks for your answers.

    I solved the problem, it was an include problem. I included an old
    version of the base class in another file.

    mfg
    , Jan 23, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Lawrence

    Polymorphism in GridView (ASP.Net 2.0)

    Kevin Lawrence, Feb 16, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    840
    Kevin Lawrence
    Feb 16, 2006
  2. Krivenok Dmitry
    Replies:
    13
    Views:
    1,429
    Axter
    Jun 1, 2006
  3. Replies:
    2
    Views:
    280
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Jul 27, 2006
  4. Jonas Huckestein
    Replies:
    3
    Views:
    347
    Rolf Magnus
    Feb 12, 2007
  5. josh
    Replies:
    12
    Views:
    1,907
Loading...

Share This Page