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

Industry Leaders: 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
C++ Language error 28 310
How to copy an image file into clipboard C/C++? 1 250
computer science syllabus 3 115
Installshield for Embarcadero EX 10.1 Berlin 4 90
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

734 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