[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Problem with passing struct in function

Posted on 2004-04-29
4
Medium Priority
?
349 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

649 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