PDA

View Full Version : Problems with QDate



cyberboy
24th May 2008, 20:55
Hi guys,

I'm busy to program a little bit more OOP in c++.
But I'm stuck with my first OOP look a like script.

The idea is a factory pattern, I have one abstract interface abstractOrderRecord.h
And a child class orderAddRecord.h and in the future order.h and some other.

This is abstractOrderRecord.h

#ifndef ORDERRECORD_H
#define ORDERRECORD_H

#include <QObject>

class QDate;
class QStringList;
class QString;

struct article {
QString description, customization, nr;
int ordered, delivered;
float price;
QDate *warrentyDate;
};

class OrderRecord : public QObject
{
Q_OBJECT
public:
//set methods
virtual void setOrderId(const int);
virtual void setCid(const int);
virtual void setDeadline(const QDate );
virtual void setPrepayment(const float);
virtual void setPreviousState(const int);
virtual void setCurrentState(const int);
virtual void addArticle(const QString, const QString, const QString, const float, const int, const int, const QDate);
virtual void addArticle(const struct article);

//accessors
virtual int getOrderId();
virtual int getCid();
virtual QDate getDeadline();
virtual float getPrepayment();
virtual int getPreviousState();
virtual int getCurrentState();
virtual article getArticle(int);

};

#endif

This is orderAddRecord.h


#ifndef ORDERADDRECORD_H
#define ORDERADDRECORD_H

#include <QObject>
#include <QWidget>

#include "AbstractOrderRecord.h"

class QDate;
class QStringList;

class OrderAddRecord : public QObject, public virtual OrderRecord {
Q_OBJECT
public:
OrderAddRecord(QWidget *parent = 0);
void setOrderId(const int);
void setCid(const int);
void setDeadline(const QDate);
void setPrepayment(const float);
void setPreviousState(const int);
void setCurrentState(const int);
void addArticle(const struct article);
void addArticle(const QString, const QString, const QString, const float, const int, const int, const QDate);

private:
int orderId;
int cid;
QDate *deadline;
float prepayment;
int previousState;
int currentState;
QStringList *articles;
};

#endif


This is orderAddRecord.cpp



#include "orderAddRecord.h"
#include <QtCore>



OrderAddRecord::OrderAddRecord(QWidget *parent)
: QObject(parent)
{
this->deadline = new QDate();
this->articles = new QStringList();
}

void OrderAddRecord::setOrderId(const int orderId)
{
this->orderId = orderId;
}

void OrderAddRecord::setCid(const int cid)
{
this->cid = cid;
}

void OrderAddRecord::setDeadline(const QDate deadline)
{
this->deadline = deadline;
}

void OrderAddRecord::setPrepayment(const float amount)
{
this->prepayment = amount;
}

void OrderAddRecord::setPreviousState(const int state)
{
this->previousState = state;
}

void OrderAddRecord::setCurrentState(const int state)
{
this->currentState = state;
}

void OrderAddRecord::addArticles(const struct article Article)
{
this->articles << Article;
}

void OrderAddRecord::addArticles(const QString description, const QString nr, const QString customization, const float price, const int delivered, const int ordered, const QDate &warrentyDate)
{
article tmpArticle;
tmpArticle.description = description;
tmpArticle.nr = nr;
tmpArticle.customization = customization;
tmpArticle.price = price;
tmpArticle.delivered = delivered;
tmpArticle.ordered = ordered;
tmpArticle.warrentyDate = new QDate();
tmpArticle.warrentyDate = warrentyDate;

this->articles << tmpArticle;

}


And this is the error I get over and over again.


warning: direct base 'QObject' inaccessible in 'OrderAddRecord' due to ambiguity
error: cannot convert 'const QDate' to 'QDate*' in assignment
error: no 'void OrderAddRecord::addArticles(article)' member function declared in class 'OrderAddRecord'
error: no match for 'operator<<' in '((OrderAddRecord*)this)->OrderAddRecord::articles << Article'
error: prototype for 'void OrderAddRecord::addArticles(QString, QString, QString, float, int, int, const QDate&)' does not match any in class 'OrderAddRecord'
error: candidate is: void OrderAddRecord::addArticles(article)
error: cannot convert 'const QDate' to 'QDate*' in assignment
error: no match for 'operator<<' in '((OrderAddRecord*)this)->OrderAddRecord::articles << tmpArticle'


So I'm doing something terrible wrong.
After I changed the warrentyDate to *warrentyDate in the structure definition I got an error less to worry about.

So I assume that QDate variables has to be an pointer, but how can I copy the QDate passed in the function to the local warrentyDate or deadline variable.

And I can't get the structure working in the function, does somebody know how to pass structures as arguments in a function?

Many thanks,

Cyberboy

jpn
24th May 2008, 22:36
Please, go read a decent C++ book. Using Qt without C++ knowledge is a pain. You may search the forums for good books. There are some free and good online books.



warning: direct base 'QObject' inaccessible in 'OrderAddRecord' due to ambiguity

You cannot multi-inherit QObject.



error: cannot convert 'const QDate' to 'QDate*' in assignment

Just like it says, you are trying to assign an object to a pointer.



error: no 'void OrderAddRecord::addArticles(article)' member function declared in class 'OrderAddRecord'

addArticles() in .cpp vs. addArticle() in .h



error: no match for 'operator<<' in '((OrderAddRecord*)this)->OrderAddRecord::articles << Article'

It should be "QList<article> articles", not "QStringList* articles" if you wanted it to work like that.



error: prototype for 'void OrderAddRecord::addArticles(QString, QString, QString, float, int, int, const QDate&)' does not match any in class 'OrderAddRecord'

addArticles() in .cpp vs. addArticle() in .h



error: candidate is: void OrderAddRecord::addArticles(article)

...



error: cannot convert 'const QDate' to 'QDate*' in assignment

Same as few steps above.



error: no match for 'operator<<' in '((OrderAddRecord*)this)->OrderAddRecord::articles << tmpArticle'

Same as few steps above.

cyberboy
24th May 2008, 23:06
I'm sooo ashamed.:rolleyes:
I just read an article about pointers and references and I saw the big mistake, and the typo's where toooo bad!

I'm really sorry that I posted such stupid questions on this forum, maybe I have to think twice before posting.

But many thanks for the useful information and the QList <article> part was very useful because I didn't know that.

BTW, I'm currently reading "Qt 4, Einführung in die Applikationsentwicklung". And that's a good book that I can recommend to any beginner.

Many thanks,

Cyberboy

cyberboy
25th May 2008, 22:08
So, the typos are fixed and some other things too.

But now I'm having some trouble with an vTable error. And after searching the net for hours and trying over and over again I though it would be time to ask here.

This is a quote from http://www.storkyak.com/2006/07/gnu-c-undefined-reference-to-vtable.html


GNU C++: Undefined reference to 'vtable for
This is a linker error for GNU. If you get Undefined reference to 'vtable for className, it means that you declared but did not implement a virtual method. In my case, I declared a virtual destructor in the .h, but didn't implement it the .cpp.


So I thought that would be simple, just find the virtual method that I didn't implement in the subclass.
But I can't find it!:confused:

I even tried this, I removed OrderAddRecord.h and linked orderAddRecord.cpp to AbstractOrderRecord.h and I removed every virtual function and copied the private variables from orderAddRecord.h into AbstractOrderRecord.h. I changed the constructor in orderAddWindow.cpp.

But that doesn't work neither.

This is AbstractOrderRecord.h



#ifndef ORDERRECORD_H
#define ORDERRECORD_H

#include <QObject>

class QDate;
class QStringList;
class QString;

struct article {
QString description, customization, nr;
int ordered, delivered;
float price;
QDate *warrentyDate;
};

class OrderRecord : public QObject
{
Q_OBJECT
public:
//set methods
virtual void setOrderId(const int);
virtual void setCid(const int);
virtual void setDeadline( QDate* );
virtual void setPrepayment(const float);
virtual void setPreviousState(const int);
virtual void setCurrentState(const int);
virtual void addArticle(const QString, const QString, const QString, const float, const int, const int, QDate*);
virtual void addArticle(QList<article>);

//accessors
virtual int getOrderId();
virtual int getCid();
virtual QDate getDeadline();
virtual float getPrepayment();
virtual int getPreviousState();
virtual int getCurrentState();
virtual article getArticle(int);

};

#endif


This is orderAddRecord.h

#ifndef ORDERADDRECORD_H
#define ORDERADDRECORD_H

#include "AbstractOrderRecord.h"

class QDate;
class QVariant;

class OrderAddRecord : public virtual OrderRecord {
Q_OBJECT
public:
OrderAddRecord(QWidget *parent);
void setOrderId(const int);
void setCid(const int);
void setDeadline(QDate*);
void setPrepayment(const float);
void setPreviousState(const int);
void setCurrentState(const int);
void addArticle(const QString, const QString, const QString, const float, const int, const int, QDate*);
void addArticle(const QList<article>);

//accessors
int getOrderId();
int getCid();
QDate getDeadline();
float getPrepayment();
int getPreviousState();
int getCurrentState();
article getArticle(int);

private:
int orderId;
int cid;
QDate *deadline;
float prepayment;
int previousState;
int currentState;
QList<QVariant> *articles;
};

#endif

This is orderAddRecord.cpp


#include "orderAddRecord.h"
#include <QtCore>

OrderAddRecord::OrderAddRecord(QWidget *parent)
{
qDebug("OrderAddRecord::OrderAddRecord(): constructing");
}


void OrderAddRecord::setOrderId(const int orderId)
{
this->orderId = orderId;
}

void OrderAddRecord::setCid(const int cid)
{
this->cid = cid;
}

void OrderAddRecord::setDeadline(QDate *deadline)
{
this->deadline = deadline;
}

void OrderAddRecord::setPrepayment(const float amount)
{
this->prepayment = amount;
}

void OrderAddRecord::setPreviousState(const int state)
{
this->previousState = state;
}

void OrderAddRecord::setCurrentState(const int state)
{
this->currentState = state;
}

void OrderAddRecord::addArticle(QList<article> art)
{
//this->articles << art;
}

void OrderAddRecord::addArticle(const QString description, const QString nr, const QString customization, const float price, const int delivered, const int ordered, QDate *warrentyDate)
{

article tmpArticle;
tmpArticle.description = description;
tmpArticle.nr = nr;
tmpArticle.customization = customization;
tmpArticle.price = price;
tmpArticle.delivered = delivered;
tmpArticle.ordered = ordered;
tmpArticle.warrentyDate = warrentyDate;

//this->articles << tmpArticle;

}


int OrderAddRecord::getCid()
{
return this->cid;
}

int OrderAddRecord::getOrderId()
{
return this->orderId;
}

QDate OrderAddRecord::getDeadline()
{
// return this->deadline;
}

float OrderAddRecord::getPrepayment()
{
return this->prepayment;
}

int OrderAddRecord::getPreviousState()
{
return this->previousState;
}

int OrderAddRecord::getCurrentState()
{
return this->currentState;
}

article OrderAddRecord::getArticle(int index)
{

}



This is a piece of orderAddWindow.cpp

OrderRecord *record = new OrderAddRecord(this);
I tried this too.

OrderAddRecord *record = new OrderAddRecord(this);

And this is de compiler error:

"vtable for OrderRecord", referenced from:
__ZTV11OrderRecord$non_lazy_ptr in orderAddRecord.o
"vtable for OrderAddRecord", referenced from:
__ZTV14OrderAddRecord$non_lazy_ptr in orderAddRecord.o


Does somebody know how to solve this.
Or where I can read some tutorials to solve this by myself?

Thanks,

Cyberboy

P.S. I'm currently busy with this online book :http://www.icce.rug.nl/documents/cplusplus/cplusplus.html

Shadowfiend
25th May 2008, 22:17
Make sure you've re-run qmake in the directory where that file is. Usually I see vtable errors when qmake hasn't picked up that your file needs to have moc run on it (i.e., when it hasn't picked up that you have the Q_OBJECT macro).