473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

A problem with polymorphism

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 '07 #1
4 1831
<ma******@gmx.dewrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
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

Jan 19 '07 #2

ma******@gmx.de 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

Jan 19 '07 #3

Jim Langston wrote:
<ma******@gmx.dewrote 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();
}

Jan 19 '07 #4
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 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
10
by: Danny Ni | last post by:
Hi, I was asked a question in a recent interview, where in ASP.Net is polymorphism used? I got differrent answers from different people, One said ToString() method because it is working for...
35
by: JKop | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en- us/vccore98/HTML/_core_using_strict_type_checking.asp Pay particular attention to: The types WPARAM, LPARAM, LRESULT, and void *...
8
by: Locia | last post by:
What are the techniques used to implement parametric polymorphism in C++? A Polymorphic method can be virtual?
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
7
by: doina | last post by:
Hello, Can I have runtime polymorphism using references. I knew that runtime polymorphism could be obtained only by pointers, but now I have tried this in Visual C++ 8.0: #include <iostream>...
13
by: Praveen | last post by:
trying to learn plymorphism. My sample is public class Class1 { public static void Main(string args) { Cls1 x = new Cls1(); Cls2 y = new Cls2(); Cls3 y = new Cls3();
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.