LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1135|回复: 7

简单程序,不知道错在哪里? (QT程序)

[复制链接]
发表于 2006-5-21 03:24:58 | 显示全部楼层 |阅读模式
#include <qapplication.h>
#include <qpushbutton.h>

class myWidget:public QWidget
{
        Q_OBJECT
        public:
                myWidget(QWidget *parent = 0, const char *name = 0);
        public slots:
                void shutDown();
        private:
                QPushButton *exit;
};

myWidget::myWidget(QWidget *parent, const char *name)Widget(parent, name)
{
        setMinimumSize(120, 180);
        setMaximumSize(120, 180);
        exit = new QPushButton("ShutDown!",this);
        connect(exit,SIGNAL(clicked()),this,SLOT(shutDown()));
}

void myWidget::shutDown()
{
        system("halt");
}

int main(int argc, char **argv)
{
        QApplication app(argc, argv);
        myWidget *w = new myWidget();
        w->show();
        app.exec();
}
 楼主| 发表于 2006-5-21 03:25:41 | 显示全部楼层
QT4编Ñ不通Ç~~

Ð示

root@style:~/Desktop/shutdown# make
g++ -Wl,-rpath,/usr/lib/qt/lib -o shutdown shut.o    -L/usr/lib/qt/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11 -lm
shut.o(.text+0x21): In function `myWidget::myWidget[not-in-charge](QWidget*, char const*)':
: undefined reference to `vtable for myWidget'
shut.o(.text+0x28): In function `myWidget::myWidget[not-in-charge](QWidget*, char const*)':
: undefined reference to `vtable for myWidget'
shut.o(.text+0x141): In function `myWidget::myWidget[in-charge](QWidget*, char const*)':
: undefined reference to `vtable for myWidget'
shut.o(.text+0x148): In function `myWidget::myWidget[in-charge](QWidget*, char const*)':
: undefined reference to `vtable for myWidget'
collect2: ld returned 1 exit status
make: *** [shutdown] 错ï 1
root@style:~/Desktop/shutdown#
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-21 18:26:39 | 显示全部楼层
¡有º帮我???
回复 支持 反对

使用道具 举报

发表于 2006-5-22 21:34:27 | 显示全部楼层
我过去源代码安装zhcon的时候就遇到过,似乎不是语法问题。
我记得我当时好像装了一个xxx-dev,然后重新./comfigure就可以make了

你可以google搜索undefined reference to 看看。
回复 支持 反对

使用道具 举报

发表于 2006-5-24 18:55:58 | 显示全部楼层
据试验只需要在你的文件最后加一行:

  1. #include "shut.moc"
复制代码

再编译运行用可以了
回复 支持 反对

使用道具 举报

发表于 2006-5-25 19:42:44 | 显示全部楼层
最好要把该拆分的东西拆分了,照我这样拆分之后直接用常规方法即可正常编译得到可以执行的程序
正确的写法应该是这样的:
main.cpp:

  1. #include <qapplication.h>
  2. #include "mywidget.h"

  3. int main(int argc, char **argv)
  4. {
  5.     QApplication app(argc, argv);
  6.     myWidget *w = new myWidget();
  7.     w->show();
  8.     app.exec();
  9. }
复制代码

mywidget.h:

  1. #ifndef MYWIDGET_H
  2. #define MYWIDGET_H
  3. #include <qpushbutton.h>

  4. class myWidget :public QWidget
  5. {
  6. Q_OBJECT
  7. public:
  8.        myWidget(QWidget *parent = 0);
  9. public slots:
  10.        void shutDown();
  11. private:
  12.         QPushButton *exit;
  13. };

  14. #endif//MYWIDGET_H
复制代码

mywidget.cpp:

  1. #include "mywidget.h"
  2. myWidget::myWidget(QWidget *parent):QWidget(parent)
  3. {
  4.                            setMinimumSize(120, 180);
  5.                            setMaximumSize(120, 180);
  6.                            exit = new QPushButton("ShutDown!",this);
  7.                            connect(exit,SIGNAL(clicked()),this,SLOT(shutDown()));
  8. }

  9. void myWidget::shutDown()
  10. {
  11.      system("halt");
  12. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-25 23:28:49 | 显示全部楼层
谢谢,
我在主ý数中

app.exec();   //return app.exec();
改后Ø是不行,所å按6楼ô的做了ok.谢谢大¶`~
回复 支持 反对

使用道具 举报

发表于 2006-5-26 21:28:46 | 显示全部楼层
另外下次把代码帖在帖子中时得用一个[code]和[/code]扩起来,就像我那样前面会显示一个“代码”,这样既美观又用避免把:P显示为的情况
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表