Solved

Problem with passing struct in function

Posted on 2004-04-29
4
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

724 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