Go Down

Topic: structure types (Read 291 times) previous topic - next topic

PieterP

#15
Aug 01, 2019, 06:01 am Last Edit: Aug 01, 2019, 06:02 am by PieterP
Before you can create (instantiate) a "Car" object, the compiler must know what parts it's made of, and how much memory it has to reserve for it. You do this by writing down the declaration:

Code: [Select]
struct Car {
  const char *name;
  unsigned int cost;
};

You cannot create any "Car"s before the declaration is finished, i.e. before the closing bracket }.
is this how?
Car model[10] = {car1,car2,car3,};
That's not a declaration, that's the instantiation of an array of ten "Car" objects.

notsolowki

none of this makes sense to me, can you please just show me in the code i posted how to what i'm trying to do

arduino_new

so what am i missing in the definition of model? Show me how to properly define it please
Here you go:

Code: [Select]

#include <iostream>
#include <string.h>

struct Car {
   char name[20];
   int cost;
};

class CarOperator {
   public:
   Car models[10];
   CarOperator() : models {{"RedCar", 10},{"BlueCar", 20}} //initialize the array, you can add more initialization
   {
   }
   Car& operator[](int index)
   {
      return models[index];
   }
};


int main()
{
    CarOperator carOp;
    std::cout << carOp[0].name << std::endl;
    strcpy(carOp[0].name, "YellowCar");
    std::cout << carOp[0].name << std::endl;
}


Note 1: The provided overload [] bracket is for ease of access the array. If you don't like/understand it. You could access your array as:
Code: [Select]

carOp.models[0].name;


Note 2: code is written for PC because it takes less effort to run. But it should run the same for Arduino once you replace the printing statements;and put code in main() in your setup()

PieterP

none of this makes sense to me, can you please just show me in the code i posted how to what i'm trying to do
Then please tell us in a clear way what it is you're trying to do?

notsolowki

#19
Aug 01, 2019, 06:15 am Last Edit: Aug 01, 2019, 06:16 am by notsolowki
Then please tell us in a clear way what it is you're trying to do?
Apparently i don't know what i'm trying to do

hopefully you can just tell by looking at this,

struct Cars {
  public:
    String name;
    Cars model[10] // this dont work whats the right way to define this obvious array in the obvious class of Cars
    unsigned long cost;
    Cars() : name("mycar"), cost(2222) {}
}cars;

PieterP

#20
Aug 01, 2019, 06:20 am Last Edit: Aug 01, 2019, 06:20 am by PieterP
Apparently i don't know what i'm trying to do

hopefully you can just tell by looking at this,

struct Cars {
  public:
    String name;
    Cars model[10] // this dont work whats the right way to define this obvious array in the obvious class of Cars
    unsigned long cost;
    Cars() : name("mycar"), cost(2222) {}
}cars;
No, I cannot.

You cannot use the type "Cars" inside of the definition of what "Cars" is.

Me: what is a "Cars" object made of?
You: something with a name, a cost, and ten more "Cars".
Me: okay, what are the ten "Cars" objects made of?
You: a name, a cost, and ten more "Cars".
Me: okay, what are those ten "Cars" objects made of?
...

I showed you a way to do it using the "static" keyword a few posts back.

notsolowki

No, I cannot.

I showed you a way to do it using the "static" keyword a few posts back.
The way it was explained to me,

HDD data;  would create a object  in type HDD. the part i dont understand is what do they mean type. Do they mean the class HDD? i just dont know how to read it

notsolowki

#22
Aug 01, 2019, 06:26 am Last Edit: Aug 01, 2019, 06:26 am by notsolowki
struct Cars {
  public:
 
    String name;
    unsigned long cost;
    Cars() : name("mycar"), cost(2222) {}
};

struct SomeRandomStructure {
  public:
  Cars model[10]; 
}sRs;


if it works in SomeRandomStructure  why dont it work in Cars? and what is the proper way to do what happening in SomeRandomStructure in the Cars class

PieterP

#23
Aug 01, 2019, 06:39 am Last Edit: Aug 01, 2019, 06:41 am by PieterP
A struct or a class is a user-defined type.
Code: [Select]
struct A {
  // ...
};

A a;


The object "a" is an instance of class "A", the type of "a" is "A".

Compare this to
Code: [Select]
int i;
Same for arrays:
Code: [Select]
A as[10];

"as" is an array of ten instances of class "A".

if it works in SomeRandomStructure  why dont it work in Cars? and what is the proper way to do what happening in SomeRandomStructure in the Cars class
Because you have to finish declaring what "Cars" is before you can use it. Please re-read my previous reply and try to explain what part you don't understand.
A recursive definition that relies on itself is useless to the compiler.

notsolowki

#24
Aug 01, 2019, 06:48 am Last Edit: Aug 01, 2019, 06:55 am by notsolowki
so when i called   Cars model[10]; it created an array of 10 instances of cars inside model array? its starting to kind of make sense. therefor model[1] has instance of name and cost. model[2]etc.?


so i could think of it like

model1-car-name/cost
model2-car-name/cost

Wouldnt this technically create 11 instances? Cars model[10];

notsolowki

#25
Aug 01, 2019, 07:01 am Last Edit: Aug 01, 2019, 07:07 am by notsolowki
Okay so when im reading the tutorial and it says,

Box Box1;        // Declare Box1 of type Box

this basically create another instance of struct Box and names it struct Box1?

PieterP

so when i called   Cars model[10]; it created an array of 10 instances of cars inside model array? its starting to kind of make sense. therefor model[1] has instance of name and cost. model[2]etc.?
More or less, yes.

so i could think of it like

model1-car-name/cost
model2-car-name/cost
I don't know what you mean.

Wouldnt this technically create 11 instances? Cars model[10];
No. It creates ten instances: model[0] is the first one, and model[9] is the last one.

Okay so when im reading the tutorial and it says,

Box Box1;        // Declare Box1 of type Box

this basically create another instance of struct Box and names it struct Box1?
Yes.

gfvalvo

Glad I tapped out of this one early.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Romonaga


Go Up