Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, March 2002
Problem encountered in writing a class which extends QList


Message 1 in thread

I need a new Class which extends QList. I have the following code but get error:

rule.h:
#ifndef RULE_H
#define RULE_H
#include <qlist.h>

class Rule : public QList {
public:
     Rule();
     ~Rule();
};
#endif

rule.cpp:
#include "rule.h"
Rule::Rule(){
}
Rule::~Rule(){
}


Message 2 in thread

Hello,

Next time please supply the error you get.  Anyhow, I guess the problem is 
here:

> Rule::Rule(){
> }

You forgot to specify the QList constructor.  Maybe you want something like 
this:

Rule::Rule() : QList() {
}

-Justin


Message 3 in thread

I need a new Class which extends QList. I have the following code but get error:

rule.h:
#ifndef RULE_H
#define RULE_H
#include <qlist.h>

class Rule : public QList {
public:
Rule();
~Rule();
};
#endif

rule.cpp:
#include "rule.h"
Rule::Rule(){
}
Rule::~Rule(){
}



Message 4 in thread

Firstly, you need to define your Rule class constructor as

Rule::Rule(void) : QList(void) {
}

since you have derived from QList.


Dung Patrick wrote:
> 
> I need a new Class which extends QList. I have the following code but get error:
> 
> rule.h:
> #ifndef RULE_H
> #define RULE_H
> #include <qlist.h>
> 
> class Rule : public QList {
> public:
> Rule();
> ~Rule();
> };
> #endif
> 
> rule.cpp:
> #include "rule.h"
> Rule::Rule(){
> }
> Rule::~Rule(){
> }
> 
> --
> List archive and information: http://qt-interest.trolltech.com


Message 5 in thread

Am Samstag, 2. März 2002 05:36 schrieb Dung Patrick:
> I need a new Class which extends QList. I have the following code but get
> error:

Hi,

what error do you get? Which OS are you running? Which QT version do you use? 
As you are using QList I suppose you're using at most QT 2.3.x as in QT 3.0 
QList seems to only be left over as an undocumented alias for QPtrList 
(defined in pqtrlist.h). So back to QT 2.3:

QList is a template class, so if you really want/need to subclass it, you 
have to do it as a template as well:

#include <qlist.h>

template<class T>
class Rule : public QList<T>
{
public:
  Rule();
  ~Rule();
};

template<class T>
Rule::Rule() 
{
}

template<class T>
Rule::~Rule()
{
}

You don't need a base class initializer in the constructor because QList<T> 
has a default constructor.

If you don't know what I'm talking about, what C++ templates are or how 
they're used, I recommand to get you a good book on C++.


greetings,
P.J.

-- 
 [ signature omitted ] 

Message 6 in thread

Thanks for reply.

I get 2 syntax errors:
syntax error before `::'
in the cpp file (the :: in constructor/destructor)
I use qt 2.3.1, Linux.

I now has these files but get problems( Same as yours but different name...)

chain.h:
#include <qlist.h>
template<class T> class Chain: public QList <T> {
public:
Chain();
~Chain();
};

chain.cpp:
#include <chain.h>
template <class T> Chain::Chain() {}
template <class T> Chain::~Chain() {}

Patrick

Am Samstag, 2. März 2002 05:36 schrieb Dung Patrick:
> I need a new Class which extends QList. I have the following code but get
> error:

Hi,

what error do you get? Which OS are you running? Which QT version do you use? 
As you are using QList I suppose you're using at most QT 2.3.x as in QT 3.0 
QList seems to only be left over as an undocumented alias for QPtrList 
(defined in pqtrlist.h). So back to QT 2.3:

QList is a template class, so if you really want/need to subclass it, you 
have to do it as a template as well:

#include <qlist.h>

template<class T>
class Rule : public QList<T>
{
public:
  Rule();
  ~Rule();
};

template<class T>
Rule::Rule()
{
}

template<class T>
Rule::~Rule()
{
}

You don't need a base class initializer in the constructor because QList<T>
has a default constructor.

If you don't know what I'm talking about, what C++ templates are or how
they're used, I recommand to get you a good book on C++.


greetings,
P.J.

--
 [ signature omitted ] 

Message 7 in thread

Am Sonntag, 3. März 2002 16:29 schrieb Dung Patrick:
> Thanks for reply.
>
> I get 2 syntax errors:
> syntax error before `::'
> in the cpp file (the :: in constructor/destructor)
> I use qt 2.3.1, Linux.
>

sorry, my fault. Here is the correct version (notice the Rule<T> class name)

#include <qlist.h>

template<class T>
class Rule : public QList<T>
{
public:
  Rule();
  ~Rule();
};
template<class T>
Rule<T>::Rule()
{
}
template<class T>
Rule<T>::~Rule()
{
}

greetings,
P.J.

-- 
 [ signature omitted ] 

Message 8 in thread

Thanks, that works.

I found out that there's no point in spiliting the template class into .h and .cpp. Else, other classes cannot use the template class (compile error).

Patrick

-----Original Message-----
From: "P.J. Meisch" <pjmeisch@web.de>
To: qt-interest@trolltech.com
Date: Sun, 3 Mar 2002 22:57:02 +0100
Subject: Re: Re: Problem encountered in writing a class which extends QList

Am Sonntag, 3. März 2002 16:29 schrieb Dung Patrick:
> Thanks for reply.
>
> I get 2 syntax errors:
> syntax error before `::'
> in the cpp file (the :: in constructor/destructor)
> I use qt 2.3.1, Linux.
>

sorry, my fault. Here is the correct version (notice the Rule<T> class name)

#include <qlist.h>

template<class T>
class Rule : public QList<T>
{
public:
  Rule();
  ~Rule();
};
template<class T>
Rule<T>::Rule()
{
}
template<class T>
Rule<T>::~Rule()
{
}

greetings,
P.J.

-- 
 [ signature omitted ]