www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - assigning a struct object to an array

reply "Reflexive" <alexgyg msn.com> writes:
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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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 17
dmd 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
parent reply "Reflexive" <alexgyg msn.com> writes:
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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent "Reflexive" <alexgyg msn.com> writes:
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.

 Ali
I use the pdf format I downloaded at dlang.org/.
Aug 08 2015