emanuel1337
emanuel1337

Reputation: 57

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];

Upvotes: 0

Views: 107

Answers (1)

LihO
LihO

Reputation: 42083

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";

Upvotes: 5

Related Questions