Link to home
Start Free TrialLog in
Avatar of jewee
jewee

asked on

Problem with passing struct in function

I think I implemented this incorrectly.

I have a struct defined

struct Data1 {
  std::string dataType;
  std::string procType;
  int priority;
  std::string time1Type;
}

I have 2 functions...one which sets the data in the struct.  Then I make a function call in which I pass in the struct...

void DataProc::setData()
{
    .....process...
   Data1 dt;
   dt.dataType = "type1";
   dt.procType = "proc2";
....etc...

displayData(&dt);
}

void DataProc::displayData(Data1 *dt)
{
    std::cout<<" dataType "<<dt.dataType<<endl;
....etc...

}

When I try to compile it, I get errors stating that "request for member dataType in dt, which is of non-aggregate type 'Data1'.

 
Avatar of Member_2_1001466
Member_2_1001466

change

void DataProc::displayData(Data1 *dt)

to

void DataProc::displayData(struct Data1 *dt)

or you need to typedef the struct:

typedef struct Data1 {
[snip]
} _Data1;
ASKER CERTIFIED SOLUTION
Avatar of Sys_Prog
Sys_Prog
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Steh,

Whatever u suggested is true in case of a C compiler
In C++, no need to mention the struct keyword while declaring a variable

Amit
Avatar of Axter
You can also fix this by using reference type instead of a pointer.
void DataProc::displayData(Data1 &dt)  //Reference
{
   std::cout<<" dataType "<<dt.dataType<<endl;
....etc...

}