我有两个类一和二。两者都运行线程。第二类是对第一类声明的函数进行线程化。这是通过在第二类的run方法中调用它来完成的。我试图调用/启动线程两个在一个的构造函数中,以便这两个线程一起运行。 我得到范围错误。因为缺少语法。代码如下给出C++在第一类的构造函数中创建第二类对象 - 多线程
#include <QtGui>
#include<iostream>
using namespace std;
class One:public QThread
{
public:
One()
{
Two b; // error: 'Two' was not declared in this scope error: expected ';' before 'b'
b.start();//error: 'b' was not declared in this scope|
b.wait();
};
void run();
void beep();
};
void One::run()
{
}
void One::beep()
{
}
class Two:public QThread
{
public:
Two()
{
};
void run();
};
void Two::run()
{
One a;
a.beep();
}
int main(int argc,char* argv[])
{
One a;
a.start();
a.wait();
return(0);
}
代码旁边注释中给出的错误是。
error: 'Two' was not declared in this scope
error: expected ';' before 'b'
error: 'b' was not declared in this scope
我缺少什么语法?
解决方案的实际点是由Erik提供的http://stackoverflow.com/questions/5230444/c-qthread-同时启动2线程 – 2011-03-08 12:40:14