在另一个类函数中使用一个类的变量 [英] using a class's variable in another class function
问题描述
我有这样的类
class A : public QDialog
{
A(QWidget *parent);
static map<int,QString> dataa;
};
class B : public QThread
{
public void run();
}
class C : public QDialog
{
public void abc();
}
类的定义
void B::run()
{
in this function,i am using __dataa__ as __dataa.insert__which is defined initially in A class as public.
}
void C::abc()
{
in this function also,i am using __dataa__ as __dataa.find__ and dataa.end__ which is defined initially in A class as public.
}
现在请帮助我如何在其他两个类中使用该变量dataa B和C。在某个地方,我看到我们必须将其声明为静态,并在B和C类中将其用作A :: data,但无法正常工作并给出错误。
Now please help me how can i use that variable dataa in the other two classes B and C.somewhere i seen we have to declare it as static and use it in the B and C class as A::data but its not working and giving error.
这三个类被初始化为
int main()
{
A window;
B datathread;
datathread.start
//B is Qthread inherited and A,C are QDialog inherited
}
通过在类A上单击按钮并在新窗口中打开其对话框,可以从类A初始化类C。
i这次我猜我没有明确提出我的问题不会受到指责。由于我长期以来一直对此感到困惑,请神赐予我帮助。
class C is initialized from class A by clicking a pushbutton on it and opening its dialog in a new window. i guess this time i wont be blamed for not clearly formulating my question.please on gods name help me as i am stuck over this from long time.a big thanks in advance for any kind of help.
推荐答案
对A :: dataa的未定义引用
undefined reference to A::dataa
您需要在A的.cpp文件中定义它。说:
You need to define it in A's .cpp file. Say:
std::map<int, QString> A::dataa;
还要确保可以在其他地方访问它,有两种选择:
Also to make sure this is accessible elsewhere there are two options:
- 在A类的
public
部分中声明此地图对象 - 或通过@Jeeva建议的函数返回该对象的副本。
- declare this map object in the
public
section of A class - or have a copy of this object returned via a function like @Jeeva suggests.
现在,要以其他单位访问此变量,您将首先包括A类的头文件。
Now, to access this variable in other units, you would first include the header file for A class.
#include "A.h" //or something similar..
并访问它:
void B::run() //and similar with C::run(..)
{
A::dataa //do something with it ..
}
这篇关于在另一个类函数中使用一个类的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!