constructor

Calling ambiguously overloaded constructor in Java

五迷三道 提交于 2019-12-11 03:04:19
问题 I just saw this C# question and wondered, if something similar could happen in Java. It can, with class A<T> { A(Integer o) {...} A(T o) {...} } the call new A<Integer>(43); is ambiguous and I see no way how to resolve it. Is there any? 回答1: Yes, members of a parameterized type JLS3#4.5.2 can end up in conflicts that are precluded in a normal class declaration(#8.4.8). It's pretty easy to come up with many examples of this kind. And in Java, neither constructor in your example is more

Thrown object cannot be caught in a multi-threaded solution

安稳与你 提交于 2019-12-11 03:03:11
问题 I have a RAII class that solves a problem in an inner thread: #include <iostream> #include <thread> using namespace std; struct solution_using_thread { solution_using_thread() : alive_(true), thread_() { thread_ = thread([this]() { while(alive_); }); } ~solution_using_thread() { alive_ = false; thread_.join(); } private: bool alive_; thread thread_; }; int main() { cout << 0 << endl; try { solution_using_thread solution; throw 1; } catch (int i ) { cout << i << endl; } cout << 2 << endl; }

Constructor in class cannot be applied to given types. Hope for assistance

落爺英雄遲暮 提交于 2019-12-11 02:43:51
问题 I'm fairly new to Java and I'm using BlueJ . I keep getting the error: constructor ItemNotFound in class ItemNotFound cannot be applied to given types; required: int found: no arguments reason: actual and formal arguments lists differ in length I'm fairly confused and in turn not sure how to fix the problem. Hopefully someone can help me. Thank you in advance. Here is my class Catalog: public class Catalog { private Item[] list; private int size; // Construct an empty catalog with the

Ambiguity in c++ constructor when a constructor with default argument exists

泪湿孤枕 提交于 2019-12-11 02:39:45
问题 #include<iostream> using namespace std; class abc{ int a; public: abc() { } //do nothing constructor abc(int x=6){ a=x;} //constructor with default argument }; main() { abc a; .... } my question is which constructor will be invoked in this case ? Please explain 回答1: This will not compile due to the ambiguity as you can see here prog.cpp:8:7: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type] prog.cpp: In function ‘int main()’: prog.cpp:10:11: error: call of overloaded

Initialization of virtual base class of abstract class

瘦欲@ 提交于 2019-12-11 02:38:49
问题 Consider the code: #include <iostream> using std::cout; using std::endl; struct A { virtual void foo(){ }; A() { cout << "A()" << endl; } }; struct B : virtual A { virtual void bar() = 0; B() : A() //mem-initializer of virtual base class { cout << "B()" << endl; } }; struct C : B { void bar(){ }; C() : B() { cout << "C()" << endl; } }; C c; int main() { //A() //B() //C() //Is output } demo I've written the code to understand the rule's note from 12.6.2/8 N37973 : [Note: An abstract class (10

Scala: Use multiple constructors from Java in Scala

戏子无情 提交于 2019-12-11 02:22:23
问题 I want to use a Jar in Scala, that is written in Java. There are classes, that have multiple constructores, for example: public LabeledDock(Parent<? super Labeled> parent, int index, Class<?> subtype){} and public LabeledDock(Parent<? super Labeled> parent, Class<?> subtype) So the first constructor has 3 inputs, the second only 2 inputs. If i want to use these constructors in Scala in that way: val button = new LabeledDock(scene.asParent(), classOf[Button]) Scala tells me that "ambiguous

error: constructor Player in class Player cannot be applied to given types;

≡放荡痞女 提交于 2019-12-11 02:19:36
问题 whenever I compile my code, I receive the following errors: error: constructor Player in class Player cannot be applied to given types; but it doesn't list any types. The code in question is public class Team { private String name; public Player players[]; public Player temp; public Team(String inputname, Player players[]) { inputname = name; this.players = new Player [players.length]; for( int k=0 ; k<players.length ; k++ ) this.players[k] = new Player(players[k]); //This is the line with

Adding code in constructor with alternative class syntax

孤街醉人 提交于 2019-12-11 02:11:13
问题 type Foo = class inherit Bar val _stuff : int new (stuff : int) = { inherit Bar() _stuff = stuff } end I want to add this code in above constructor: if (stuff < 0) then raise (ArgumentOutOfRangeException "Stuff must be positive.") else () How can I achieve this in F# ? 回答1: You can do this without needing any workarounds, but the placement of the initial left curly is fairly sensitive (or maybe the parser is buggy?). To do the effect first: type Foo = class inherit Bar val _stuff : int new

Call parent constructor in java

Deadly 提交于 2019-12-11 02:05:09
问题 I have two class Parent and Child , while the Parent have a constructor which needs 3 arguments: class Parent{ public Parent(String host,String path,int port){ } } And now I want the Child constructor need only one argument, then I try to do something like this: class Child extend Parent{ public Child(String url){ String host=getHostFromUrl(url); String path=.... String port=... super(host,path,port); } } But this does not work. Any idea to fix it? BTW, I have no access to Parent class. 回答1:

C++ no appropriate default constructor available

谁说胖子不能爱 提交于 2019-12-11 02:00:53
问题 I have some experience with C# but C++ syntax and program construction makes some problems. I am using Visual C++ 2008. Firstly why is there this error?: 1>......\Form1.h(104) : error C2512: 'Cargame::Car' : no appropriate default constructor available Secondly, why is not this line possible? //System::Drawing::Color color; error C3265: cannot declare a managed 'color' in an unmanaged 'Car' Form1.h contains: namespace Cargame { using namespaces bla bla bla class Car; public ref class Form1 :