constructor

Error when initializing a struct with a brace-enclosed initializer list

杀马特。学长 韩版系。学妹 提交于 2021-01-27 02:37:40
问题 struct CLICKABLE { int x; int y; BITMAP* alt; BITMAP* bitmap; CLICKABLE() { alt=0; } }; CLICKABLE input={1,2,0,0}; This code gives me the following error: Could not convert from brace-enclosed initializer list Could someone explain me why the compiler is giving me this error, and how I can fix it? I'm still learning the language. 回答1: Your class has a constructor, so it isn't an aggregate, meaning you cannot use aggregate initialization. You can add a constructor taking the right number and

How to create System.IO.StreamWriter with Encoding in Powershell?

丶灬走出姿态 提交于 2021-01-24 11:59:29
问题 I'm trying to create instance of StreamWriter with UTF8 encoding in PowerShell. $f = New-Object System.IO.StreamWriter "a.txt", $false, [System.Text.Encoding]::UTF8 This throws error: New-Object : Cannot find an overload for "StreamWriter" and the argument count: "3". I'm trying to invoke this constructor: https://msdn.microsoft.com/en-us/library/f5f5x7kt(v=vs.110).aspx 回答1: Still, I would like to know what is wrong with my original syntax. Your original syntax (fundamentally correctly) uses

Construct tuple by passing the same argument to each element with explicit constructor

天大地大妈咪最大 提交于 2021-01-23 06:51:12
问题 The following works fine on Visual C++ 2015 Update 2. Note that A is non-copyable and A::A is explicit . #include <iostream> #include <tuple> struct A { explicit A(int i) { std::cout << i << " "; } // non-copyable A(const A&) = delete; A& operator=(const A&) = delete; }; template <class... Ts> struct B { std::tuple<Ts...> ts; B(int i) : ts((sizeof(Ts), i)...) { } }; int main() { B<A, A, A, A> b(42); } The goal is to pass the same argument to all tuple elements. It correctly outputs: 42 42 42

Is a constructor __init__ necessary for a class in Python?

萝らか妹 提交于 2021-01-21 08:28:56
问题 I read that the constructor is like the first argument passed to the class, which makes sense to me since the parameters seem to be passed to the class via the __init__ method. For example, class NewsStory(object): def __init__(self, guid, title, subject, summary, link): self.guid = guid self.title = title self.subject = subject self.summary = summary self.link = link def get_guid(self): return self.guid def get_title(self): return self.title def get_subject(self): return self.subject def get

Is a constructor __init__ necessary for a class in Python?

时光怂恿深爱的人放手 提交于 2021-01-21 08:28:28
问题 I read that the constructor is like the first argument passed to the class, which makes sense to me since the parameters seem to be passed to the class via the __init__ method. For example, class NewsStory(object): def __init__(self, guid, title, subject, summary, link): self.guid = guid self.title = title self.subject = subject self.summary = summary self.link = link def get_guid(self): return self.guid def get_title(self): return self.title def get_subject(self): return self.subject def get

TypeScript complain “has no initializer and is not definitely assigned in the constructor” about constructors by returning constructed object

旧巷老猫 提交于 2021-01-21 07:03:52
问题 TypeScript show following error message to this code samples: class MyClass { someField: boolean; constructor() { return { someField: true }; } } Property 'someField' has no initializer and is not definitely assigned in the constructor. (property) MyClass.someField: boolean TypeScript Playground (You need to enable strictNullChecks and strictPropertyInitialization to see this error message.) Given code snippet is simplified from my original script. I would like to return the constructed value

TypeScript complain “has no initializer and is not definitely assigned in the constructor” about constructors by returning constructed object

怎甘沉沦 提交于 2021-01-21 07:02:24
问题 TypeScript show following error message to this code samples: class MyClass { someField: boolean; constructor() { return { someField: true }; } } Property 'someField' has no initializer and is not definitely assigned in the constructor. (property) MyClass.someField: boolean TypeScript Playground (You need to enable strictNullChecks and strictPropertyInitialization to see this error message.) Given code snippet is simplified from my original script. I would like to return the constructed value

Abstract class and unique pointer

一个人想着一个人 提交于 2021-01-21 03:48:48
问题 I have the following error in my code: error: allocating an object of abstract class type 'Material' I don't know how to handle this case. I'm aware that std::make_unique performs an allocation, so it can't allocate the object of type Material , but I don't know how to correct it. #include <iostream> #include <memory> struct Material { Material() = default; virtual int get_color() const = 0; }; struct Basic : public Material { Basic() = default; virtual int get_color() const override { return

Abstract class and unique pointer

自古美人都是妖i 提交于 2021-01-21 03:45:10
问题 I have the following error in my code: error: allocating an object of abstract class type 'Material' I don't know how to handle this case. I'm aware that std::make_unique performs an allocation, so it can't allocate the object of type Material , but I don't know how to correct it. #include <iostream> #include <memory> struct Material { Material() = default; virtual int get_color() const = 0; }; struct Basic : public Material { Basic() = default; virtual int get_color() const override { return

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

爱⌒轻易说出口 提交于 2021-01-20 17:18:28
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they