简体   繁体   中英

How to Access a Struct Inside a Struct in C++?

I'm trying to access the variables string ModelName and int Sales like this Dealer.Modelo.ModelName but it's not working. How can I access those variables to fill the the structure?

PD: The compiler says that "Dealer" should have a class type.

const int MAXIMODEALERS = 20;
const int MAXIMOMODELOS = 6;
struct Detail
{
    string ModelName;
    int Sales;
};

struct Element
{
        string   CompanyName;
        Detail  Modelo[MAXIMOMODELOS];
};

Element Dealer[MAXIMODEALERS];
Element Dealer[MAXIMODEALERS];

declares an array of objects of type Element , but :

Dealer.Modelo.ModelName = "something";

is treating Dealer as it would be a single instance of Element , neither an array. You need to use an index to access concrete element (same for the Modelo ):

Dealer[SomeIndex].Modelo[OtherIndex].ModelName = "something";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM