Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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


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??
Posted
Comments
CPallini 20-Jan-16 12:42pm    
Could you please show the code you use for checking the value?
XamBEE 20-Jan-16 12:48pm    
just trying to check the value
std::cout << WorkerThreadClass::WorkerThreadStr.SyncTimeMilliSec << "\n";
Sergey Alexandrovich Kryukov 20-Jan-16 13:29pm    
It should not even compile because SyncTimeMilliSec is an instance (non-static) member.
—SA

1 solution

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;


[EDIT]
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]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 20-Jan-16 18:28pm    
5ed.
—SA
XamBEE 21-Jan-16 3:45am    
AS usual Thanks!!
XamBEE 21-Jan-16 5:03am    
how one can initialize all members of struct? if i want to initialize all integer members with zero.
Jochen Arndt 21-Jan-16 5:40am    
If you have a struct and want all of the members to be initialised with zero, use:
struct_type val = { 0 };

This will set the first member to the specified value (here zero) and all remaining members to zero.

If you want to do this always upon creation, define a constructor for the struct (a structure in C++ is nothing else then a class with all members being public by default):
struct S
{
 S() : x(0), y(0){}
 int x, y;
};

See also: http://en.cppreference.com/w/c/language/struct_initialization

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900