default-constructor

No default constructor found exception

[亡魂溺海] 提交于 2019-12-24 02:55:13
问题 I'm developing a web application in java spring 4 framework. At one point I'm using FormDataContentDisposition class which is providing by jersey. This class used at an endpoint of my REST call like this , @RequestMapping(value = "/createArticle/", method = RequestMethod.POST) @Consumes(MediaType.MULTIPART_FORM_DATA) public ResponseEntity<Void> createNewArticle(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetails) when i make a

How do I make define and declare a variable using the default constructor in C++?

可紊 提交于 2019-12-23 22:39:33
问题 From my understanding of declarations and definitions, at the global scope: MyClass instance();//Declares a function that returns a MyClass MyClass instance;//Declares an instance of MyClass Is it possible to declare a variable and define it to use the default constructor at global scope? What if I was using a structure instead of a class? EDIT: Okay, so MyClass instance; does call the default constructor. Can anyone explain how this is consistent with this example: int a; // not default

Explicitly defaulted constructors and initialisation of member variables

梦想与她 提交于 2019-12-23 18:06:07
问题 I'm slightly confused about what happens when a ctor is explicitly defaulted. Are the two code samples below equivalent? Are there any constraints on Y to be able to use the first option? class X { public: X() = default; private: Y m_y; }; class X { public: X() : m_y() {} private: Y m_y; }; 回答1: There are two possible sources of differences. X() = default; is not user-provided . X() : m_y() {} is. The former can be trivial ; the latter is never trivial. Also, they will behave differently if

Why does a struct, that has another struct wrapped in a union as a member not compile without an explicit default constructor?

房东的猫 提交于 2019-12-23 17:01:39
问题 This is the relationship I am talking about: struct A{ int i = 1; }; struct B{ union{A a;}; }; void main(){ B b; }; In this constellation, my compiler (vs2015) complains about the default constructor of B B::B(void) beeing deleted, with the note that the compiler has generated B::B : ../test.cpp(155): error C2280: "B::B(void)" : Es wurde versucht, auf eine gelöschte Funktion zu verweisen ../test.cpp(152): note: Compiler hat hier "B::B" generiert (sorry, I could not convince msvc to talk

Java: Instantiating a generic class with no default constructor

放肆的年华 提交于 2019-12-23 12:27:50
问题 I am trying to do this: public class BaseTable<T extends TableEntry> { protected int mRows; protected int mCols; protected ArrayList<T> mEntries; public BaseTable(int rows, int cols) { mRows = rows; mCols = cols; mEntries = new ArrayList<T>(); for (int i = 0; i < rows; i++) { mEntries.add(new T(cols)); //this obv. doesn't work } } } Instantiating generics is hard enough as it is, but what makes this even harder is that T here does not have a default constructor, it takes a single int

C++: Creating an uninitialized placeholder variable rather than a default object

拜拜、爱过 提交于 2019-12-23 08:55:44
问题 I'm moving from Java to C++ right now and I'm having some difficulties whenever a commonly used concept in Java doesn't map directly into C++. For instance, in Java I would do something like: Fruit GetFruit(String fruitName) { Fruit fruit; if(fruitName == "apple") fruit = new Fruit("apple"); else if(fruitName == "banana") fruit = new Fruit("banana"); else fruit = new Fruit("kumquat"); //'cause who really wants to eat a kumquat? return fruit; } Of course, in C++ the Fruit fruit; statement

Inherited constructors, default constructor and visibility

老子叫甜甜 提交于 2019-12-23 07:49:10
问题 As stated by [namespace.udecl]/18: [...] A using-declaration that names a constructor does not create a synonym; instead, the additional constructors are accessible if they would be accessible when used to construct an object of the corresponding base class, and the accessibility of the using-declaration is ignored. [...] Because of that, the following code does not compile: class B { protected: B(int) { } }; class D: B { using B::B; }; int main () { D d{0}; } It returns an error that is more

C++ standard list and default-constructible types

青春壹個敷衍的年華 提交于 2019-12-23 05:38:11
问题 Why is that the single parameter constructor of std::list<T> requires T to be a default-constructible type? I mean the following code does not compile. struct Foo { // does not have default constructor. Foo (int i) {} } int main(void) { std::list<Foo> l(10); } It seems possible to use the construct and destroy idioms as they have already done in the std::vector, albeit with more book-keeping the list class. On a related note, why not have the capacity function in list? You can argue that such

Empty constructor in c++

蹲街弑〆低调 提交于 2019-12-23 04:34:06
问题 Well I understand the part that I will be getting some random value, but is the Foo() constructor in the snippet acting just like the default public constructor which the compiler supplies when we have no constructor defined? #include<iostream> using namespace std ; class Foo{ int i ; public: Foo(){ } void disp(){ cout<<"i = "<<i ; } }; int main(){ Foo bar1,bar2 ; bar1.disp(); cout<<"\n"; bar2.disp(); } I have seen some people writing an empty constructor like this, but I could't understand

Member of Union has User-Defined Constructor

旧巷老猫 提交于 2019-12-23 02:24:07
问题 For the following code: class Foo{ int foo; public: Foo() : foo(13) {} int getFoo() const { return foo; } }; union Bar{ Foo fBar; double dBar; }; I believe this is fully legal in C++. http://en.cppreference.com/w/cpp/language/union#Explanation says: If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler And thus in gcc I can do this: Bar bar = { Foo() } When I try this in Visual Studio 2008 I get the error: error C2620: member Bar