• 8056阅读
  • 7回复

【提问】使用QThread类中出现的问题 [复制链接]

上一主题 下一主题
离线xd1198
 

只看楼主 倒序阅读 楼主  发表于: 2005-10-09
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
源代码如下:
  1. /* *****     thread.h   ************/
  2. #include <qthread.h>
  3. //#include <unistd.h>
  4. //#include "form1.h"
  5. class MyThread : public QThread
  6. {
  7. public:
  8.     MyThread();
  9.     virtual void run();
  10.     ~MyThread();
  11. };
  12. /*************   thread.cpp **********/
  13. #include "thread.h"
  14. void MyThread::run()
  15. { while(1){
  16.       printf("123\n");
  17.       sleep(1);
  18.       printf("321\n");
  19.       sleep(1);
  20.       }
  21. }[font=楷体_GB2312][/font]
  22. MyThread::MyThread()
  23. {
  24. }
  25. MyThread::~MyThread()
  26. { }

调试信息如下:
thread.h:6: parse error before `{' token
thread.h:9: virtual outside class declaration

thread.h:10: destructors must be member functions
thread.h:11: parse error before `}' token
thread.cpp:4: invalid use of undefined type `class MyThread'
thread.h:5: forward declaration of `class MyThread'

thread.cpp: In member function `void MyThread::run()':
thread.cpp:7: `sleep' undeclared (first use this function)
thread.cpp:7: (Each undeclared identifier is reported only once for each
  function it appears in.)
thread.cpp: At global scope:
thread.cpp:14: invalid use of undefined type `class MyThread'
thread.h:5: forward declaration of `class MyThread'
thread.cpp:17: invalid use of undefined type `class MyThread'
thread.h:5: forward declaration of `class MyThread'

主要问题出在黑体的调试信息部分,
” class MyThread : public QThread “是第六行。为什么这里会出错了?假如我把QThread 换成QApplication 等别的类,此处的报错就会没有。
还有一个问题是:run在此处必须是virtual的吗?
[ 此贴被xd1198在2005-10-09 12:52重新编辑 ]
离线myer

只看该作者 1楼 发表于: 2005-10-11
可能你的编译选项没有包含thread模块。
http://user.qzone.qq.com/56430808
离线sky_hao

只看该作者 2楼 发表于: 2005-10-20
1.是的,你在生成的.pro文件的最后一行加上CONFIG +=thread warn_on 再试试
2.不必要,因为run()再QThread类中已经声明为是纯虚函数了
离线wjydlut

只看该作者 3楼 发表于: 2005-11-26
"1.是的,你在生成的.pro文件的最后一行加上CONFIG +=thread warn_on 再试试
2.不必要,因为run()再QThread类中已经声明为是纯虚函数了"
什么意思呀?
离线712100

只看该作者 4楼 发表于: 2006-04-22
加了也不行.我试过了,我也遇见了这个问题.
离线efengyu

只看该作者 5楼 发表于: 2006-04-27
呼唤达人出来
离线lubin008
只看该作者 6楼 发表于: 2007-12-17
回 楼主(xd1198) 的帖子
请问楼主如何解决的?小弟遇到了这样的问题
离线foxyz

只看该作者 7楼 发表于: 2007-12-18
请检查你的QtThread引用是否保证正确!或者说你要用到的头文件能被compiler找到吗?
另外QtThread注意大小写。这个实体在QtCore library里
快速回复
限100 字节
 
上一个 下一个