未定义的引用“vtable for XXX”

时间:2015-11-04 15:01:24

标签: c++ qt qobject

我正在尝试连接两个对象,第一个QPushButton,第二个继承QGraphicsTextItem。我有一个问题,我不知道如何解决它。告诉我它会是什么?

costofbet.h

#ifndef COSTOFBET_H
#define COSTOFBET_H

#include <QGraphicsTextItem>

class CostOfBet : public QGraphicsTextItem
{
    Q_OBJECT

private:
    int bet;

public:
    explicit CostOfBet(QGraphicsTextItem * parent = 0);
    int getBet()const;
    void setBet(int _bet);
    void increaseBet();
    void decreaseBet();

public slots:
    void increaseBetSlot();
    void decreaseBetSlot();
};

#endif // COSTOFBET_H

costofbet.cpp

#include "costofbet.h"
#include <QFont>

CostOfBet::CostOfBet(QGraphicsTextItem * parent)
    : QGraphicsTextItem()
{
    bet = 0;
    setPlainText(QString("CSTOFBET:\n") + QString::number(bet) + QString(" $"));
    setDefaultTextColor(Qt::darkGreen);
    setFont(QFont("carocalcy", 16));
}

int CostOfBet::getBet() const
{
    return bet;
}

void CostOfBet::setBet(int _bet)
{
    if(_bet < 0)
        return;

    bet = _bet;
}

void CostOfBet::increaseBet()
{
    bet += 5;
}

void CostOfBet::decreaseBet()
{

    if(bet - 5 < 0)
        return;

    bet -= 5;
}

void CostOfBet::increaseBetSlot()
{
    increaseBet();
}

void CostOfBet::decreaseBetSlot()
{
    decreaseBet();
}

game.h

#ifndef GAME_H
#define GAME_H

#include <QGraphicsScene>
#include <QGraphicsView>

class Game : public QGraphicsView
{
private:
    QGraphicsScene * scene;

protected:
    virtual void wheelEvent (QWheelEvent * event);

public:
    Game(QWidget *parent = 0);
    ~Game();

};

#endif // GAME_H

game.cpp

#include "game.h"
#include <QGraphicsRectItem>
#include <QGraphicsWidget>
#include <QGraphicsGridLayout>
#include <QEvent>
#include <QDebug>
#include "costofbet.h"
#include <QPushButton>

Game::Game(QWidget *parent)
    : QGraphicsView(parent)
{
    scene = new QGraphicsScene();

    //create 3 items to place game mechanism
    QGraphicsRectItem * top = new QGraphicsRectItem;
    QGraphicsRectItem * middle = new QGraphicsRectItem;
    QGraphicsRectItem * bottom = new QGraphicsRectItem;

    top->setRect(0,-550,400,50);
    middle->setRect(0,-500,400,400);
    bottom->setRect(0,-100,400,100);

    scene->addItem(top);
    scene->addItem(middle);
    scene->addItem(bottom);

    //add money, bet, jackpot to the top rectitem
    MoneyLeft * money = new MoneyLeft;
    money->setPos(0,-550);
    scene->addItem(money);

    CostOfBet * mybet = new CostOfBet;
    mybet->setPos(134,-550);
    scene->addItem(mybet);

    //add the buttons to the bottom sector
    QPushButton * incrButton = new QPushButton;
    QPushButton * decrButton = new QPushButton;

    incrButton->setText("Increase bet");
    decrButton->setText("Decrease bet");

    incrButton->setGeometry(0,-99,100,50);
    decrButton->setGeometry(0,-49,100,50);

    scene->addWidget(incrButton);
    scene->addWidget(decrButton);

    //make the bet work
    QObject::connect(incrButton, SIGNAL(clicked(bool)), mybet, SLOT(increaseBetSlot()));
    QObject::connect(decrButton, SIGNAL(clicked(bool)), mybet, SLOT(decreaseBetSlot()));

    //show the view
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setFixedSize(400, 550);
    setScene(scene);
    show();
}

void Game::wheelEvent(QWheelEvent * event)
{
    if (event->type() == QEvent::Wheel)
        return;
    return;
}

Game::~Game()
{

}

如何连接此插槽和信号?

QObject::connect(incrButton, SIGNAL(clicked(bool)), mybet, SLOT(increaseBetSlot()));
QObject::connect(decrButton, SIGNAL(clicked(bool)), mybet, SLOT(decreaseBetSlot()));

编译器指向这一行

    CostOfBet::CostOfBet(QGraphicsTextItem * parent)
    : QGraphicsTextItem() //an issue
{
.....;
}

并说error: undefined reference to vtable for CostOfBet。 如何解决这个问题?任何猜测都会很好。

0 个答案:

没有答案