C ++ disabled copy constructor, cannot create object

I do not provide any custom constructors for my class, all I did was disable the copy constructor:

private:
MyClass(const MyClass& other) = delete; // disable copy ctor

Now when I try to instantiate this class on the stack

MyClass myInstance;

I get a compilation error as follows:

main.cpp:16:16: error: no matching function for call toMyClass::MyClass()

As if I accidentally disabled the default constructor? Or perhaps the copy constructor is typed there, I just don't see how.

Here is an example

class MyClass {
public:
    int someField;

private:
    MyClass(const MyClass& other) = delete; // disable copy ctor
    MyClass& operator=(MyClass other) = delete; // disable assignment

};

And mistake

g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"proj/main.d" -MT"pitch/main.o" -o "proj/main.o" "../proj/main.cpp"
../proj/main.cpp: In function ‘int main()’:
../proj/main.cpp:17:10: error: no matching function for call to ‘MyClass::MyClass()’
  MyClass ins;
+4
source share
1 answer

The problem is that you do not have a default constructor, so the error is:

../proj/main.cpp: 17: 10: error: there is no corresponding function to call in MyClass :: MyClass ()

, , , . , :

(struct, class union) ,

. ( @user4581301):

, , , , default.

:

class MyClass {
public:
    MyClass() = default;
    int someField;
+5
source

Source: https://habr.com/ru/post/1693007/


All Articles