Solved

Problem with passing struct in function

Posted on 2004-04-29
4
331 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now