Re: Constructors not working

2022-09-03 Thread Mathias LANG via Digitalmars-d-learn
On Friday, 2 September 2022 at 18:35:22 UTC, Svyat wrote: I write this code in one directory: ``` module time; struct Time { public int hours, minutes, seconds; this(int h, int m, int s) { hours = h; minutes = m; seconds = s; }

Re: Constructors not working

2022-09-02 Thread Tejas via Digitalmars-d-learn
On Friday, 2 September 2022 at 18:39:27 UTC, rikki cattermole wrote: I think you are wanting opAssign not opBinary. Also you made a mistake, since its a struct you don't want to new it when you construct and return it. ```d return new Time(secos / 3600, (secos % 3600) / 60, secos % 60); ```

Re: Constructors not working

2022-09-02 Thread Ali Çehreli via Digitalmars-d-learn
I forgot to say that you don't need to write a constructor for most structs because Time's constructor-generated default constructor works like yours and with default arguments: struct Time { public int hours, minutes, seconds; // No constructor needed here. // Note 'return this;' as

Re: Constructors not working

2022-09-02 Thread Ali Çehreli via Digitalmars-d-learn
On 9/2/22 11:35, Svyat wrote: > Time time = 360; It seems to be more idiomatic to write it like this: auto time = Time(360); Or const, immutable, etc. const time = Time(360); // Now un-assignable But you would get the same compilation error. So, one way to work with it is to

Re: Constructors not working

2022-09-02 Thread rikki cattermole via Digitalmars-d-learn
I think you are wanting opAssign not opBinary. Also you made a mistake, since its a struct you don't want to new it when you construct and return it. ```d return new Time(secos / 3600, (secos % 3600) / 60, secos % 60); ``` Would be a ``Time*`` not ``Time`` which is what you returned.

Constructors not working

2022-09-02 Thread Svyat via Digitalmars-d-learn
I write this code in one directory: module time; struct Time { public int hours, minutes, seconds; this(int h, int m, int s) { hours = h; minutes = m; seconds = s; } Time opBinary(string op : "=")(int secos) {