例子如下:实现的功能:开两个线程,每个都写ping!20次,然后退出.
#include <qthread.h>
#include <qapplication.h>
#include <stdio.h>
#include <stdlib.h>
class MyThread : public QThread {
public:
virtual void run();
};
void MyThread::run()
{
for(int count=0;count<20;count++) {
sleep(1);
qDebug("Ping!");
}
}
int main()
{
MyThread a;
MyThread b;
a.start();
b.start();
a.wait();
b.wait();
}
先设置环境变量,然后运行qmake -project,然后用build脚本编译,脚本内如如下:
#!/bin/bash
source /opt/FriendlyARM/mini2440/x86-qtopia/qtopia-2.2.0-FriendlyARM/setQpeEnv
qmake -o Makefile -spec /opt/FriendlyARM/mini2440/x86-qtopia/qtopia-2.2.0-FriendlyARM/qtopia/mkspecs/qws/linux-generic-g++ *.pro
make clean
make
出错:
thread.h:6: error: expected class-name before ‘{’ token
thread.h: In member function ‘virtual void MyThread::run()’:
thread.h:17: error: ‘sleep’ was not declared in this scope
main.cpp: In function ‘int main()’:
main.cpp:10: error: ‘class MyThread’ has no member named ‘start’
main.cpp:11: error: ‘class MyThread’ has no member named ‘start’
main.cpp:12: error: ‘class MyThread’ has no member named ‘wait’
main.cpp:13: error: ‘class MyThread’ has no member named ‘wait’
make: *** [.obj/release-shared/main.o] Error 1
请高手指点迷津,在线等