constructor

“no suitable method found for add(java.lang.String)”in arraylist constructor?

三世轮回 提交于 2021-02-05 06:36:05
问题 import java.util.ArrayList; import java.util.Random; public class College { // instance variables - replace the example below with your own private ArrayList<Student> students; public College() { // initialise instance variables ArrayList<Student> students = new ArrayList<Student>(); students.add("Student 1"); students.add("Student 2"); students.add("Student 3"); } } basically it highlights the .add showing the error message "java.lang.IllegalArgumentException: bound must be positive", I don

Default constructor cannot be referenced when using std::string in union member of a struct

淺唱寂寞╮ 提交于 2021-02-04 18:28:25
问题 I have a very basic struct that has an enum and a union. typedef struct { enum v{a,b,c}v; union w{ int a; bool b; std::string c; }w; }Data_Set2; int main() { Data_Set2 val; // Shows errror that the default constructor cannot be referenced return 0; } On using such a struct I get the Error Code C2280 that the default constructor cannot be referenced. When I declare the struct in a slightly different way as following typedef struct { enum v{a,b,c}v; union w{ int a; bool b; std::string c; }; //

Ambiguous constructor call

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-04 17:53:11
问题 I'm trying to create a simple date class, but I get an error on my main file that says, "call of overloaded Date() is ambiguous." I'm not sure why since I thought as long as I had different parameters for my constructor, I was ok. Here is my code: header file: #ifndef DATE_H #define DATE_H using std::string; class Date { public: static const int monthsPerYear = 12; // num of months in a yr Date(int = 1, int = 1, int = 1900); // default constructor Date(); // uses system time to create object

How can I call async method from constructor?

空扰寡人 提交于 2021-02-04 17:08:47
问题 I need to call a async method from my Form1 constructor. Since a constructor can't have a return type, I can't add a async void . I read that static constructor can be async but I need to call methods from constructor that aren't static , such as InitializeComponent() (since it's the Form's constructor). The class is: public partial class Form1 : Form { InitializeComponent(); //some stuff await myMethod(); } I read this one too but I still don't know how to implement this (in my case) since

Using an object without copy and without a noexcept move constructor in a vector. What actually breaks and how can I confirm it?

早过忘川 提交于 2021-02-04 15:08:22
问题 I've checked a lot of move constructor/vector/noexcept threads, but I am still unsure what actually happens when things are supposed to go wrong. I can't produce an error when I expect to, so either my little test is wrong, or my understanding of the problem is wrong. I am using a vector of a BufferTrio object, which defines a noexcept(false) move constructor, and deletes every other constructor/assignment operator so that there's nothing to fall back to: BufferTrio(const BufferTrio&) =

Using an object without copy and without a noexcept move constructor in a vector. What actually breaks and how can I confirm it?

我与影子孤独终老i 提交于 2021-02-04 15:08:19
问题 I've checked a lot of move constructor/vector/noexcept threads, but I am still unsure what actually happens when things are supposed to go wrong. I can't produce an error when I expect to, so either my little test is wrong, or my understanding of the problem is wrong. I am using a vector of a BufferTrio object, which defines a noexcept(false) move constructor, and deletes every other constructor/assignment operator so that there's nothing to fall back to: BufferTrio(const BufferTrio&) =

When do we need a private constructor in C++?

岁酱吖の 提交于 2021-02-02 10:09:10
问题 I have a question about private constructors in C++. If the constructor is private, how can I create an instance of the class? Should we have a getInstance() method inside the class? 回答1: There are a few scenarios for having private constructors: Restricting object creation for all but friend s; in this case all constructors have to be private class A { private: A () {} public: // other accessible methods friend class B; }; class B { public: A* Create_A () { return new A; } // creation rights

When do we need a private constructor in C++?

三世轮回 提交于 2021-02-02 10:08:26
问题 I have a question about private constructors in C++. If the constructor is private, how can I create an instance of the class? Should we have a getInstance() method inside the class? 回答1: There are a few scenarios for having private constructors: Restricting object creation for all but friend s; in this case all constructors have to be private class A { private: A () {} public: // other accessible methods friend class B; }; class B { public: A* Create_A () { return new A; } // creation rights

Why is PHP private variables working on extended class?

不打扰是莪最后的温柔 提交于 2021-02-02 09:26:21
问题 Shouldn't it generate error when i try to set the value of a property from the extended class instead of a base class? <?php class first{ public $id = 22; private $name; protected $email; public function __construct(){ echo "Base function constructor<br />"; } public function printit(){ echo "Hello World<br />"; } public function __destruct(){ echo "Base function destructor!<br />"; } } class second extends first{ public function __construct($myName, $myEmail){ $this->name = $myName; $this-

calling constructor of a class in another class

强颜欢笑 提交于 2021-01-29 05:07:37
问题 I have two classes named info and i2 in qt. info is a c++ class and i2 is a Qt designer from class . I want to use i2 as an environment for testing my info class. I included info.h in the header of i2.my info class has two constructor like below: info(); info(const void *ip, const char* pw, const void *hostName); now I want to make an object of info in i2 class and pass those three parameters( ip and pw and hostname) to info and use them in my program. like below: private: Ui::i2 *ui; info