Problem with passing struct in function

Posted on 2004-04-29
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 which sets the data in the struct.  Then I make a function call in which I pass in the struct...

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


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


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

Question by:jewee
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
LVL 13

Expert Comment

ID: 10946930

void DataProc::displayData(Data1 *dt)


void DataProc::displayData(struct Data1 *dt)

or you need to typedef the struct:

typedef struct Data1 {
} _Data1;
LVL 10

Accepted Solution

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


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

LVL 10

Expert Comment

ID: 10946989

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

LVL 30

Expert Comment

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;



Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 77
C++ Language error 28 276
Should CArray be used for a list of pointers in C++? 19 128
designing in object programming 12 104
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

740 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