Solved

Problem with passing struct in function

Posted on 2004-04-29
4
337 Views
Last Modified: 2010-04-01
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'.

 
0
Comment
Question by:jewee
  • 2
4 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 10946930
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;
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 250 total points
ID: 10946984
void DataProc::displayData(Data1 *dt)
{
    std::cout<<" dataType "<<dt.dataType<<endl;
....etc...

}


Since dt is a pointer
    std::cout<<" dataType "<<dt.dataType<<endl;
should be
    std::cout<<" dataType "<<dt -> dataType<<endl;

Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10946989
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
0
 
LVL 30

Expert Comment

by:Axter
ID: 10947148
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...

}

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question