在qt .cpp文件中访问STRUCT alement时出错 [英] error while accessing STRUCT alement in qt .cpp file

查看:126
本文介绍了在qt .cpp文件中访问STRUCT alement时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在我的.cpp文件中获取STRUCT值,但不知道具体怎么做,因为我是第一次使用这个。

这里是我试图做的代码..

在WorkerThreadClass.h文件中

Hi,
I am trying to get STRUCT value in my .cpp file but dont know exactly how to do this because i am using this first time.
here is the code how i am trying to do..
in WorkerThreadClass.h file

class WorkerThreadClass : public QObject
{
    Q_OBJECT
public:
    explicit WorkerThreadClass(QObject *parent = 0);
    struct WorkerThreadStr
    {
    double SyncTimeMilliSec;

    }workerthreadstr;






in WorkerThreadClass.cpp file

WorkerThreadClass::WorkerThreadStr.SyncTimeMilliSec=14989897.1005; // going to assign value to the struct element





当我尝试COUT结果元素的值来检查outout是空的。如果你可以引导我的经验如何使用它会很好吗?



when i try to COUT the value of the struct element to check outout is empty. would be nice if you can guide me with your experience how to go with it??

推荐答案

参见数据结构 - C ++教程 [ ^ ]关于结构类型名称和对象名称。



在你的情况下 WorkerThreadStr 是类型, workerthreadstr 是一个对象(这里是 WorkerThreadClass 的成员)。



如果你想从类函数中赋值,你必须使用对象名:

See Data structures - C++ Tutorials[^] about structure type names and object names.

In you case WorkerThreadStr is the type and workerthreadstr is an object (here a member of WorkerThreadClass).

So you have to use the object name if you want to assign a value from a class function:
void WorkerThreadClass::SomeFunc()
{
    workerthreadstr.SyncTimeMilliSec=14989897.1005;
}



当使用类外的类型时,你必须声明一个类型的变量:


When using the type outside the class you must declare a variable of the type:

WorkerThreadClass::WorkerThreadStr workerthreadstrVal;
workerthreadstrVal.SyncTimeMilliSec=14989897.1005;







如果你有一个实例你的班级,通过以下方式访问会员:




If you have an instance of your class, access the member via that:

WorkerThreadClass threadClass;
threadClass.workerthreadstr.SyncTimeMilliSec=14989897.1005;
std::cout << threadClass.workerthreadstr.SyncTimeMilliSec << "\n";



[/ EDIT]


[/EDIT]


这篇关于在qt .cpp文件中访问STRUCT alement时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆