digitalmars.D.learn - assigning a struct object to an array
- Reflexive (38/38) Aug 07 2015 Hello
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/18) Aug 07 2015 dmd 2.068 gives a better message:
- Reflexive (4/4) Aug 07 2015 OK, I got it. Thank you very much.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/10) Aug 07 2015 Thank you very much for the kind words. Which format are you using? It
- Reflexive (2/8) Aug 08 2015 I use the pdf format I downloaded at dlang.org/.
Hello I just began to learn D. I have some experience with Visual Basic and (very little) C/C++. Last years I have been working with PHP. So, I try to make up a class representation of a 52 cards deck. This way : // sabot.d // version 0.0.1 import std.stdio ; void main(){ auto deck = new sabot ; } class sabot{ carte[] sabotarray ; this(){ int i ; for (i=1 ; i<=52 ; i++){ carte tempcarte ; tempcarte.id = i ; sabotarray[] ~= tempcarte ; // line 17 } writeln(this.sabotarray[1]) ; } struct carte { int id ; string valeur_face ; int valeur_reelle ; int couleur ; } } But I get this error : sabot.d(17): Error: cannot append type carte to type carte I dont understand. Both sabaotarray[] and tempcarte are declared as carte type error message confirms). Why aren't they compatible ? I tryed different things, but didn't get it work. I can solve the problem by declaring a static array. But I would like to understand why this dont work. Thank you
Aug 07 2015
On 08/07/2015 02:05 AM, Reflexive wrote:class sabot{ carte[] sabotarray ; this(){ int i ; for (i=1 ; i<=52 ; i++){ carte tempcarte ; tempcarte.id = i ; sabotarray[] ~= tempcarte ; // line 17dmd 2.068 gives a better message: Error: slice expression this.sabotarray[] is not a modifiable lvalue Just drop the []: sabotarray ~= tempcarte; After all, the symbol 'sabotarray' represents the array and you want to append to it. On the other hand, sabotarray[] is a temporary slice, which happens to be an rvalue (read: not modifiable). You don't want to append to that temporary. Ali
Aug 07 2015
OK, I got it. Thank you very much. Is it you who wrote "Programming in D" ? It's a great e-book, very clear, I love it. Alex
Aug 07 2015
On 08/07/2015 05:37 AM, Reflexive wrote:Is it you who wrote "Programming in D" ?Yes. (The other Ali Çehreli is a musician. :) )It's a great e-book, very clear, I love it.Thank you very much for the kind words. Which format are you using? It is good to hear that it is acceptable as an ebook, as I've always targetted HTML and then PDF for the print version (which should be purchasable "any day now"). Fortunately, the tool I used (calibre) worked great. Ali
Aug 07 2015
On Friday, 7 August 2015 at 23:01:33 UTC, Ali Çehreli wrote:Thank you very much for the kind words. Which format are you using? It is good to hear that it is acceptable as an ebook, as I've always targetted HTML and then PDF for the print version (which should be purchasable "any day now"). Fortunately, the tool I used (calibre) worked great. AliI use the pdf format I downloaded at dlang.org/.
Aug 08 2015