digitalmars.D.learn - constructor is not callable using argument types ()
- Suliman (6/6) Dec 06 2012 I am learning D classes and I am getting error when I am try to
- Simen Kjaeraas (11/16) Dec 06 2012 You have supplied the string you promised to.
- Suliman (2/2) Dec 06 2012 Am I right that my variant of code would be valid for C#?
- Suliman (5/5) Dec 06 2012 When I should use keyword this?
- Simen Kjaeraas (19/24) Dec 06 2012 Indeed. If you have not defined a constructor, the language defines one
- Suliman (3/3) Dec 08 2012 Could you help me with this simple code. I still playing with
- bearophile (31/34) Dec 08 2012 You have code like this:
- Suliman (8/8) Dec 10 2012 Thanks!
- bearophile (16/23) Dec 10 2012 There are several ways to help that. A simple way is to use
I am learning D classes and I am getting error when I am try to make instance of class. erorr: C:\code\main.d(9): Error: constructor GetFileName.GetFileName.this (string name) is not callable using argument types () http://www.everfall.com/paste/id.php?xie09xz9upth
Dec 06 2012
On 2012-12-06, 20:17, Suliman wrote:I am learning D classes and I am getting error when I am try to make instance of class. erorr: C:\code\main.d(9): Error: constructor GetFileName.GetFileName.this (string name) is not callable using argument types () http://www.everfall.com/paste/id.php?xie09xz9upthYou have supplied the string you promised to. You have this constructor: this(string name) And call it like this: new GetFileName(); Where's the string the constructor expects? Instead, you should call it like thus: new GetFileName("test"); -- Simen
Dec 06 2012
When I should use keyword this? I dropped it from my class and now I can make instance of class without in sych way: auto file = new GetFileName(); file.name = "test";
Dec 06 2012
On 2012-12-06, 20:48, Suliman wrote:When I should use keyword this? I dropped it from my class and now I can make instance of class without in sych way: auto file = new GetFileName(); file.name = "test";Indeed. If you have not defined a constructor, the language defines one for you, which is parameterless. It does nothing but allocate and initialize memory. The moment you define a constructor of your own, the compiler decides you probably want to define the parameterless constructor yourself, or not at all, and thus does not define it for you. If you want both, then define both: class MyClass { string name; this() { name = "Foo!"; } this(string name) { this.name = name; } } -- Simen
Dec 06 2012
Could you help me with this simple code. I still playing with constructor, but I can't understand how to use it. http://www.everfall.com/paste/id.php?6n72xxxkz7ba
Dec 08 2012
Suliman:Could you help me with this simple code. I still playing with constructor, but I can't understand how to use it. http://www.everfall.com/paste/id.php?6n72xxxkz7baYou have code like this: class FileName { string name; this(string name) { this.name = name; } ... void ReadFile() { auto file = new FileName(); ... As you see you define a costructor that accepts a string, but then when you allocate the class you don't give it a string. So the correct code is: void ReadFile() { auto file = new FileName("somestring"); ... More notes: - By convention in D method/function names start with a lower case. - Don't indent module-level functions (like ReadFile). - If your program is composed by more than one module, I suggest to add a name at the top of the imported module, like "module Foo;". - In D structs are used quite often. Bye, bearophile
Dec 08 2012
Thanks! But if I have a lot of property like^ string name int age .... it's not good to specify all of them in paranceses: auto file = new FileName("test.txt", 21, ETC); How can I specify them in another way?
Dec 10 2012
Suliman:But if I have a lot of property like^ string name int age .... it's not good to specify all of them in paranceses: auto file = new FileName("test.txt", 21, ETC); How can I specify them in another way?There are several ways to help that. A simple way is to use default arguments: this(string fileName, int id=21, Foo f=ETC) {...} An alternative is to define various constructors with different number of arguments. Other solutions include the use of a little configuration struct, or the chaining of setters that return "this": FileName setId(int id_) { this.id = id_; return this; } Then if you define several of such setters, you can chain only the ones you want in a single line. auto f = new FileName("test.txt"); f.setId(21).setF(ETC); Probably there are other solutions. Bye, bearophile
Dec 10 2012