declaration

How to declare and initialise an array in Swift

主宰稳场 提交于 2020-01-13 14:01:46
问题 I have a class in Swift that I'm trying to write that has a variable of an array of objects. Is there a better way to write this? var myvar: Array<MyClass> = Array<MyClass>() Without the bit after the = sign, the compiler complains about my AppDelegate having no initializers. The above way seems a bit lengthy (though it's no more terse than the c# equivalent, I guess). I'd just like to know if there's a shortcut. Thanks. 回答1: You can initialize the variable with an empty array: var myvar:

C++ Order of Declaration (in Multi-variable Declaration Line)

拟墨画扇 提交于 2020-01-12 15:18:10
问题 I use the following in my C++ code: int a = 0, b = a; I would like to know if this behaviour is reliable and well defined (left to right order of name declaration) and that my code will not break with other compilers with an undeclared name error. If not reliable, I would break the statement: int a = 0; int b = a; Thank you. 回答1: I believe the answer is no. It is subject to core active issue 1342 which says: It is not clear what, if anything, in the existing specification requires that the

C++ Order of Declaration (in Multi-variable Declaration Line)

随声附和 提交于 2020-01-12 15:17:09
问题 I use the following in my C++ code: int a = 0, b = a; I would like to know if this behaviour is reliable and well defined (left to right order of name declaration) and that my code will not break with other compilers with an undeclared name error. If not reliable, I would break the statement: int a = 0; int b = a; Thank you. 回答1: I believe the answer is no. It is subject to core active issue 1342 which says: It is not clear what, if anything, in the existing specification requires that the

Getting error: ISO C++ forbids declaration of with no type

有些话、适合烂在心里 提交于 2020-01-12 03:57:06
问题 I'm getting the following errors: ISO C++ forbids declaration of ttTreeInsert with no type ISO C++ forbids declaration of ttTreeDelete with no type ISO C++ forbids declaration of ttTreePrint with no type prototype for int ttTree::ttTreePrint() does not match any in class ttTree candidate is: void ttTree::ttTreePrint() Here is my header file: #ifndef ttTree_h #define ttTree_h class ttTree { public: ttTree(void); int ttTreeInsert(int value); int ttTreeDelete(int value); void ttTreePrint(void);

Incorrect expansion of a function-like macro

守給你的承諾、 提交于 2020-01-11 12:25:03
问题 Good evening, I have a problem with macros. I am supposed to come up with a macro ENTRY , that puts a value into an array ( scanf("%d",&ENTRY(x,i)) was given). I tried: #define ENTRY (a,b) (a[b-1]) , but that didn't work. It created a compiler error that says, that a and b are undeclared. But I thought that I don't have to declare variables used in macros, especially because, for example: #define min (a,b) ((a)<(b)?(a):(b)) worked in another program. So what am I doing wrong here? #include

Incorrect expansion of a function-like macro

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-11 12:24:05
问题 Good evening, I have a problem with macros. I am supposed to come up with a macro ENTRY , that puts a value into an array ( scanf("%d",&ENTRY(x,i)) was given). I tried: #define ENTRY (a,b) (a[b-1]) , but that didn't work. It created a compiler error that says, that a and b are undeclared. But I thought that I don't have to declare variables used in macros, especially because, for example: #define min (a,b) ((a)<(b)?(a):(b)) worked in another program. So what am I doing wrong here? #include

Creating prepopulated set in Java [duplicate]

南笙酒味 提交于 2020-01-11 08:01:39
问题 This question already has answers here : How to initialize HashSet values by construction? (23 answers) Closed 5 years ago . In Java, how do I create a final Set that's populated at construction? I want to do something like the following: static final Set<Integer> NECESSARY_PERMISSIONS = new HashSet<Integer>([1,2,3,6]); but I don't know the proper syntax in Java. 回答1: Try this idiom: import java.util.Arrays; new HashSet<Integer>(Arrays.asList(1, 2, 3, 6)) 回答2: You might consider using Guava's

Empty arrays automatically initialize contents?

别说谁变了你拦得住时间么 提交于 2020-01-11 07:21:33
问题 How come int alone; System.out.println(alone); Gives errors but int[] arr = new int[1]; System.out.println(arr[0]); Equals 0? When you initialize an empty array, does it automatically initialize its contents to 0 (or null, etc.)? 回答1: From the language standard Otherwise, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value (§4.12.5). And the default values are: For type byte, the default value is zero, that is, the

“variable declared and not used” compilation error

怎甘沉沦 提交于 2020-01-11 05:07:06
问题 I am learning Google's new language Go. I am just trying stuff out and I noticed that if you declare a variable and do not do anything with it the go compiler ( 8g in my case) fails to compile with this error: hello.go:9: error declared and not used . I was suprised at this since most language compilers just warn you about unused variables but still compile. Is there anyway I can get around this? I checked the documentation for the compiler and I don't see anything that would change this

Forward declaration & circular dependency

痴心易碎 提交于 2020-01-10 14:22:14
问题 I've got two classes, Entity and Level. Both need to access methods of one another. Therefore, using #include, the issue of circular dependencies arises. Therefore to avoid this, I attempted to forward declare Level in Entity.h: class Level { }; However, as Entity needs access to methods in Level, it cannot access such methods, since it does not know they exist. Is there a way to resolve this without re-declaring the majority of Level in Entity? 回答1: A proper forward declaration is simply: