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!

My bad, returning an array of structs

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.