User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,539 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,266 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 729 | Replies: 2
Reply
Join Date: Feb 2008
Location: Canada
Posts: 20
Reputation: abrou is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
abrou abrou is offline Offline
Newbie Poster

Help passing an array of structs from a member function

  #1  
Feb 4th, 2008
Hello, I am a fairly inexperienced programmer, and I am confusing myself.

I want to have a member function or a different class return an array of structures. I've read that I have to do this by reference, but I can seem to get it right. Can someone help?

The header file has the line:

static struct Comm* listCommands(QString directory, QString docName);
\\comm is the name of the structure

The structure is declared(?) as follows:

struct Comm //command structure
{
QString cLabel;
};
This is the member function of RoboDOM, which will be called by the class named compare. I am using Qt4, and I think the xml traversing is alright, if not I can probably figure that part out myself.

struct Comm* RoboDOM::listCommands(QString directory, QString docName)
{
QStringList transList;
QDomDocument doc = openDoc(directory, docName);
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
QDomNodeList tagList = doc.elementsByTagName("commands");

n = tagList.item(0);
QDomNodeList commandList = n.childNodes;

Comm *commands[50]; //Make an array of structs
for(int i = 0; i<commandList.count();i++)
{
n = commandList.item(i);
e = n.toElement();
QStringList newone;
newone << "Command"<<i;
Comm newone.join(" ") =
{
nameCode(e.tagName())

}
commands[i] =newone.join(" ");
}
return commands;

}
And at last, it is here called by compare. I assume this is where I'm going wrong, or maybe in what I am returning. I think I have a lot of trouble with the syntax of pointers because maybe I don't understand them very well.

Comm *listofcommands[50] = RoboDOM::listCommands(directory, protocol1);
QString Listy = listofcommands[0].cLabel;
type1->setText(Listy);

Eitherway, the error I am currently getting is:
compare.cpp:98: error: invalid initializer
compare.cpp:99: error: `cLabel' has not been declared
compare.cpp:99: error: request for member of non-aggregate type before ';' token

Thank you very much in advanced!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: Canada
Posts: 20
Reputation: abrou is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
abrou abrou is offline Offline
Newbie Poster

Re: passing an array of structs from a member function

  #2  
Feb 4th, 2008
My bad, returning an array of structs
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,500
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 275
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Question Re: passing an array of structs from a member function

  #3  
Feb 4th, 2008
Comm *listofcommands[50] = RoboDOM::listCommands(directory, protocol1);
You're almost there. Since this is already a pointer, you don't need to make it an array. And since this is a pointer, refer to it like it's a pointer:
QString Listy = listofcommands[0]->cLabel;
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC