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(Data 1 *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'.
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(Data
{
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'.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
You can also fix this by using reference type instead of a pointer.
void DataProc::displayData(Data 1 &dt) //Reference
{
std::cout<<" dataType "<<dt.dataType<<endl;
....etc...
}
void DataProc::displayData(Data
{
std::cout<<" dataType "<<dt.dataType<<endl;
....etc...
}
void DataProc::displayData(Data
to
void DataProc::displayData(stru
or you need to typedef the struct:
typedef struct Data1 {
[snip]
} _Data1;