J.A.Norton
J.A.Norton

Reputation: 115

How can I point to a struct that is defined in another class?

In my application I want to have a list with entries. Some of these list entries have a sublist with more entries. For the entries I created a class that has a string for the text and a pointer which points to the sublist. These entries are put together to a list with a vector of pointers to the entries. Now I also want to have a selection variable that holds a number. So I put that vector together with the variable in a struct.

I tried it like this:

entry.cpp

class entry
{
    struct list; //prototype

    std::string text;
    struct* ptrToSubList;
}

in main.cpp

struct list
{
    std::vector<entry*> entryVector;
    int selection;
}

As you can see the question is which to define first because both need to know of each other already when created. The example above wouldnt compile and gives following error:

connot convert entry::list* to main::list* in assignment

So, how can I point to a struct that is defined in another class? Is this bad style?

Upvotes: 0

Views: 58

Answers (1)

CustodianSigma
CustodianSigma

Reputation: 746

You need to fully qualify your list struct to tell the compiler it actually belongs to the entry class as below:

struct entry::list {
// ...
};

Upvotes: 1

Related Questions