c++在第一类的构造函数中创建第二类的对象 - 多线程

发布于 2024-10-21 07:53:31 字数 884 浏览 1 评论 0原文

我有两个班级一和二。两者都运行线程。类二是对类一中声明的函数进行线程化。这是通过在第二个类的 run 方法中调用它来完成的。我正在尝试在一个线程的构造函数中调用/启动线程两个,以便这两个线程一起运行。 由于缺少语法,我收到范围错误。下面给出了代码。

#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);

}

代码旁边的注释中给出的错误是。

错误:此处未声明“二” 范围

错误:预期为“;”在“b”之前

错误:“b”未在此声明 范围

我缺少什么语法?

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
scope

error: expected ';' before 'b'

error: 'b' was not declared in this
scope

What syntax am i missing ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ可否回来 2024-10-28 07:53:31

您的错误是由编译器尝试实例化尚未声明的类/类型引起的。

您应该将声明和实现拆分为单独的文件,最好是广泛使用的 .h 和 .cpp 格式。然后将类型的标头包含在您需要的 cpp 中。

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.

独留℉清风醉 2024-10-28 07:53:31

嗯...我可能遗漏了一些东西,但似乎你的问题是 One 的定义甚至看不到 Two 的声明;将声明移动到头文件中,例如

class One:public QThread
{
民众:
一();
无效运行();
无效蜂鸣声();
};

然后在.cpp中:
一::一()
{
两个b;
b.start();
b.等待();
};

与二类似。
这将使其构建;由于我对 QT 不太熟悉,所以我不会对此进行一般性评论

Hmm... I may be missing something, but it seems like your problem is that definition of One doesn't see even the declaration of two; move the declarations into the header file e.g.

class One:public QThread
{
public:
One();
void run();
void beep();
};

and then in .cpp:
One::One()
{
Two b;
b.start();
b.wait();
};

similarly with Two.
This will make it build; I won't comment on it in general as I'm not very familiar with QT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文