C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

using derived class in base class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jan Bernatik
Guest





PostPosted: Mon Jan 26, 2004 12:06 am    Post subject: using derived class in base class Reply with quote



hi.

I'm stuck with it for few days, and can't find a way to work it out.

I want to make a pointer to object of derived class in base class.
( I use QT + OpenGL, QGLWidget is inherited from qgl.h)
So I tried to make it like this:


----------------------------------------------------------------------
class base_object : public QGLWidget {};
class triangle : base_object {};

class base_object : public QGLWidget
{
Q_OBJECT // makro needed by QT libraries
public:
base_object( QWidget *parent = 0, const char *name = 0 );
~base_object();

void SetPtr(triangle *temp) {p_obj = temp; };
public slots:
protected:
private:

triangle *p_obj;
};

class triangle : public base_object
{
public:
triangle() { SetPtr(this);};
};

----------------------------------------------------------------------

compiler says:
redefinition of `class base_object' ...

I dont know where to look for help, I tried to find answer in
inheritance, declaration and definition of classes, in Thinking in C++
book, but I haven't found it.
Is it possible to solve it like this, or is this idea completely wrong
??

thanks for help, have a nice day.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Magnar Nedland
Guest





PostPosted: Mon Jan 26, 2004 10:28 am    Post subject: Re: using derived class in base class Reply with quote



Quote:
class base_object : public QGLWidget {};
class triangle : base_object {};

Your problem lies here, in the alledged forward declarations. These are in
fact full definitions of the two classes, thus the compiler is in its right
to tell you "redefinition of `class base_object'".
What you want is simply:

class triangle;


Magnar



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
void
Guest





PostPosted: Mon Jan 26, 2004 10:35 am    Post subject: Re: using derived class in base class Reply with quote



Użytkownik Jan Bernatik napisał, On 2004-01-26 01:06:

Quote:
hi.

I'm stuck with it for few days, and can't find a way to work it out.

I want to make a pointer to object of derived class in base class.
( I use QT + OpenGL, QGLWidget is inherited from qgl.h)
So I tried to make it like this:


----------------------------------------------------------------------
class base_object : public QGLWidget {};
class triangle : base_object {};

class base_object : public QGLWidget
{
Q_OBJECT // makro needed by QT libraries
public:
base_object( QWidget *parent = 0, const char *name = 0 );
~base_object();

void SetPtr(triangle *temp) {p_obj = temp; };
public slots:
protected:
private:

triangle *p_obj;
};

class triangle : public base_object
{
public:
triangle() { SetPtr(this);};
};

----------------------------------------------------------------------

compiler says:
redefinition of `class base_object' ...

I dont know where to look for help, I tried to find answer in

You should find help in your book.
Here we have two declarations of base_object, if really want to have
pointer to derived class in base your class you should replace this two
lines:

Quote:
----------------------------------------------------------------------
class base_object : public QGLWidget {};
class triangle : base_object {};

with this one line and it should work correctly:

class triangle;

IMHO all you need is pointer to base class and virtual methods and not
pointer tpo derived class in base class, IMHO this is very strange
constuction.

Best
Darek Ostolski
--
MORE CORE AVAILABLE, BUT NOT FOR YOU

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Mon Jan 26, 2004 10:36 am    Post subject: Re: using derived class in base class Reply with quote

Jan Bernatik wrote:
Quote:
class base_object : public QGLWidget {};
class triangle : base_object {};

class base_object : public QGLWidget
{
triangle *p_obj;
};

class triangle : public base_object
{
};

Firstly, remove the first two lines. Then, the compiler will only complain
about unknown type 'triangle' being used. Since you only need a pointer to
it, a declaration will be enough:
class trangle;

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gordon Marr
Guest





PostPosted: Mon Jan 26, 2004 10:44 am    Post subject: Re: using derived class in base class Reply with quote

Hi -

The problem here isn't inheritance; it's the fact that you've defined
both of your classes twice. Perhaps you meant the first definitions to
be forward declarations?

Try changing these lines:

Quote:
class base_object : public QGLWidget {};
class triangle : base_object {};

to:

class base_object;
class triangle;

It compiles for me with G++ 3.3.2 then, if I add definitions for the QT
stuff (no QT here).

Why do you need pointers to derived objects in your base classes?

GM.

PS. Thanks to those of you who answered my question about C++ loop
syntax - much appreciated!


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jan Bernatik
Guest





PostPosted: Mon Jan 26, 2004 2:51 pm    Post subject: Re: using derived class in base class Reply with quote

Quote:
Gordon Marr wrote:
Why do you need pointers to derived objects in your base classes?

My idea is, that I will make one base_object, with static array of
derived objects, and than I will make one method for draw all this
objects. So in the end there will be base_object.Draw method which will
draw all derived objects.
I now it can be done in different way, but I think this is not
completely wrong, or is it ? Please tell me.

Thank all of you for help. Of course it works mow.
have a nice day

John


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sergei Emantayev
Guest





PostPosted: Mon Jan 26, 2004 8:32 pm    Post subject: Re: using derived class in base class Reply with quote

[email]bernatik (AT) kn (DOT) vutbr.cz[/email] (Jan Bernatik) wrote in message news:<24f76b1f.0401251029.5eb49cc0 (AT) posting (DOT) google.com>...
Quote:
hi.

I'm stuck with it for few days, and can't find a way to work it out.

I want to make a pointer to object of derived class in base class.
( I use QT + OpenGL, QGLWidget is inherited from qgl.h)
So I tried to make it like this:


----------------------------------------------------------------------
class base_object : public QGLWidget {};
class triangle : base_object {};


Just put forward declaration, not class definition!

class triangle;
class base_object : public QGLWidget
{
...
void SetPtr(triangle *temp) {p_obj = temp; };
...
};

class triangle : public base_object
{
...
};


Quote:

thanks for help, have a nice day.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Krügler (nee Spang
Guest





PostPosted: Tue Jan 27, 2004 2:44 pm    Post subject: Re: using derived class in base class Reply with quote

Good Morning Jan or John,

Jan Bernatik schrieb:

Quote:
----------------------------------------------------------------------
class base_object : public QGLWidget {};
class triangle : base_object {};

class base_object : public QGLWidget
{
Q_OBJECT // makro needed by QT libraries
public:
base_object( QWidget *parent = 0, const char *name = 0 );
~base_object();

void SetPtr(triangle *temp) {p_obj = temp; };
public slots:
protected:
private:

triangle *p_obj;
};

class triangle : public base_object
{
public:
triangle() { SetPtr(this);};
};

----------------------------------------------------------------------

compiler says:
redefinition of `class base_object' ...


The compiler expresses quite clearly what is wrong. Both the base_object

class and the triangle
class are redefined in the same translation unit (Additionally comes:
These definitions are
violationg the ODR - one definition rule, but that second problem is
another one)

First you write

class base_object : public QGLWidget {};

which **defines** the base_object class. Later on you write

class base_object : public QGLWidget
{
//...
}

which also defines the base_object class (and additionally this time differently
from the first). The very same problem applies to the multiple definitions of the
triangle class.

What you actually need to do is to **declare** the triangle class by
using the syntax

class triangle;

before the definitions of the 2 mentioned classes. Then everything should be fine.

Greetings from Bremen,

Daniel






[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gordon Marr
Guest





PostPosted: Tue Jan 27, 2004 3:02 pm    Post subject: Re: using derived class in base class Reply with quote

Jan Bernatik wrote:
Quote:
Gordon Marr wrote:
Why do you need pointers to derived objects in your base classes?

My idea is, that I will make one base_object, with static array of
derived objects, and than I will make one method for draw all this
objects. So in the end there will be base_object.Draw method which
will draw all derived objects.
I now it can be done in different way, but I think this is not
completely wrong, or is it ? Please tell me.

Sounds like it would work, but you'd have to have an array
for each type of derived object, though. If you have time,
try looking up virtual member functions in your C++ book.
The FAQ for this group discusses the subject, too.

GM.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.