tags:

views:

73

answers:

5

Hi,

I am using Qt 4.5 so do C++. I have a class like this

 class CClass1
    {
         private:
               struct stModelDetails
               {
                      QString name;
                      QString code;
                      ..... // only variables and no functions over here
               };
               QList<stModelDetails> m_ModelDetailsList;        

         public:
               QList<stModelDetails> getModelDetailsList();
               ...               
    };

In this I have functions that will populate the m_ModelDetailsList;

I have another class say CClassStructureUsage, where I will call the getModelDetailsList() function. Now my need is that I have to traverse the QList and obtain the name, code from each of the stModelDetails.

Now the problem is even the CClass1's header file is included it is not able to identify the type of stModelDetails in CClassStructureUsage. When I get the structure list by

QList<stModelDetails> ModelList = obj->getModelInformationList();

it says stModelDetails : undeclared identifier.

How I can able to fetch the values from the structure? Am I doing anything wrong over here?

+3  A: 

Since struct stModelDetails is private, it is not visible from outside the class. You should declare it in the public section of your class instead:

class CClass1
{
     private:
           QList<stModelDetails> m_ModelDetailsList;        

     public:
           struct stModelDetails
           {
                  QString name;
                  QString code;
                  ..... // only variables and no functions over here
           };

           QList<stModelDetails> getModelDetailsList();
           ...               
};
Péter Török
@liaK depending on what you want to expose from stModelDetails you might as well make a class out of it and protect private data with accessors
Harald Scheirich
A: 

The problem is that you declared stModelDetails as a private class. Putting it in the public section should fix your problem.

Job
+4  A: 

You need to use the fully qualified name CClass1::stModelDetails. Now it will tell you it is private :)

Troubadour
A: 

There are two problems: 1. Already mentioned is that you need to move stModelDetails to the public section of your class. 2. Because it is nested, the proper name for it outside of the class is CClass1::stModelDetails.

If you really need access to it from the outside, you may want to consider whether it should be a member of CClass1 or if it should be a stand alone class or struct. I usually only use nested classes/structs when they are an implementation detail of my class.

Matt T
+2  A: 

You've already gotten a couple of suggestions for how to attack your problem directly. I, however, would recommend stepping back for a moment to consider what you're trying to accomplish here. First of all, you've said you only really want the name member of each stModelDetails item. Based on that, I'd start by changing the function to return only that:

QList<QString> GetModelDetailNames();

or, quite possibly:

QVector<QString> GetModelDetailNames();

The former has a couple of good points. First, it reduces the amount of data you need to copy. Second, it keeps client code from having to know more implementation details of CClass1. The latter retains those advantages, and adds a few of its own, primarily avoiding the overhead of a linked list in a situation where you haven't pointed to any reason you'd want to use a linked list (and such reasons are really fairly unusual).

The alternative to that is to figure out why outside code needs access to that much of CClass1's internal data, and whether it doesn't make sense for CClass1 to provide that service directly instead of outside code needing to access its data.

Jerry Coffin
No i have to obtain all the values from the struct. I just gave an example in the code. But your second point is a good valid one. I will think of it. But is it a good practice as having only the struct declaration in a header file and including it wherever needed? Take my question as an example for the scenario.
liaK
@liaK:well, if you're going to need the whole struct, it's certainly a good idea to define that struct, yes.
Jerry Coffin
@Jerry Coffin, I'll mark yours as the answer for the approach you told based on the access of data..
liaK