I have two classes one and two . Both run threads. class Two is to thread a function declared in class one . this is done by calling it in the run method of the second class. I am trying to call/start the thread Two in constructor of one so that both these threads run together.
i get scope error .due to missing syntax. the code is given below
#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);
}
The errors as given in comments next to the code are .
error: ‘Two’ was not declared in this
scopeerror: expected ‘;’ before ‘b’
error: ‘b’ was not declared in this
scope
What syntax am i missing ?
Your error are caused by compiler trying to instantiate class/type that hasn’t been declared.
You should split your declaration and implementation into separate file, preferably the widely used .h and .cpp format. Then include the type’s header in cpp where you need them.