PDA

View Full Version : Inhertiting QPtrList



sunil.thaha
7th February 2006, 11:14
Hi Guys,
I want to save the contents of the QPtrList to the Database.
So Here is what i did



//DataList.h
template <typename T>
class DataList : public QPtrList< T > {

public:
DataList(){};
virtual ~DataList(){};
bool save();
int lastError() const;

private:
void setLastError( int iError );

private:
int m_iLastError;
};




//DataList.cpp
template < typename T >
DataList<T>::DataList()
:m_iLastError( QueryResult::ErrorNotSet ) {}

template < typename T >
bool DataList<T>::save() {
DEBUG_WHERE();
for ( T* pDataItem = first(); pDataItem; next() ) {
bool bOperationFailed = true;
if( pDataItem->indicator() == DataListItem::Inserted ) {
bOperationFailed = pDataItem->insert();

} else if( pDataItem->indicator() == DataListItem::Updated ) {
bOperationFailed = pDataItem->update();

} else if( pDataItem->indicator() == DataListItem::Deleted ) {
bOperationFailed = pDataItem->remove();
}

if( bOperationFailed ) {
setLastError( pDataItem->lastError() );
return false;
}
}
return true;
}


But g++ gives me the Following Error
BusinessLogic/DataList.cpp: In member function `bool DataList<T>::save()':
BusinessLogic/DataList.cpp:10: error: there are no arguments to `first' that depend on a template parameter, so a declaration of `first' must be available
BusinessLogic/DataList.cpp:10: error: (if you use `-fpermissive', G++ will accept your code, but allowing theuse of an undeclared name is deprecated)
BusinessLogic/DataList.cpp:10: error: there are no arguments to `next' that depend on a template parameter, so a declaration of `next' must be available

Now where I am i going wrong
Please Help

jacek
7th February 2006, 16:32
Try:
for ( T* pDataItem = QPtrList< T >::first(); pDataItem; pDataItem = QPtrList< T >::next() ) {
// ...
}

sunil.thaha
8th February 2006, 04:46
Thanks Jacek,
But where did u refer this ?. Or which c++ book has this explained

Cesar
8th February 2006, 12:00
Correct me, if I'm mistaken, but this code




//DataList.h
template <typename T>
class DataList : public QPtrList< T > {

public:
DataList(){};
// [Skip]
};




//DataList.cpp
template < typename T >
DataList<T>::DataList()
:m_iLastError( QueryResult::ErrorNotSet ) {}
//[Skip]



should lead to link error, as Datalist<T>::Datalist() is implemented twice: both in header and in .cpp files. I suggest something like this:


//DataList.h
template <typename T>
class DataList : public QPtrList< T > {
public:
DataList();
// [Skip]
};

//DataList.cpp
template < typename T >
DataList<T>::DataList() :
QPtrList<T>(),
m_iLastError( QueryResult::ErrorNotSet )
{
}
//[Skip]

sunil.thaha
8th February 2006, 13:34
Correct me, if I'm mistaken, but this code
those Errors were resolved





//DataList.cpp
template < typename T >
DataList<T>::DataList() :
QPtrList<T>(),
m_iLastError( QueryResult::ErrorNotSet )
{
}
//[Skip]

But do we need to call the Base Constructor Explicity

Cesar
8th February 2006, 13:41
But do we need to call the Base Ponter Explicity
Hm... Frankly saying, I'm not sure, anyway that won't hurt ;) . I suppose the behaviour is compiler-dependent, so doing something explicitly should make the code more portable... I guess :o

sunil.thaha
8th February 2006, 13:51
No I don't think so. I have read that When a DErived object is created, the Derived Construtor will call the default constructor of the Base

jacek
8th February 2006, 15:21
But where did u refer this ?
I was just using common sense. ;)