-->

How to use defaulted and deleted functions C++

2019-07-18 04:25发布

问题:

I recently tried to learn about the defaulted and deleted functions in C++ 11 and i wrote the sample code below. When I try to run it says :

error C2065: 'default' : undeclared identifier

the Code :

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

struct A
{
    int age;

    A(int x): age(x){};
    A() = default;

};

int _tmain(int argc, _TCHAR* argv[])
{
    A test(10);
    cout << test.age << endl;
    return 0;
}

回答1:

It seems that you are using Microsoft Visual Studio. I'm sorry but the Microsoft compiler doesn't allow this new syntax even in the new version VC11.

Check the list of the available features here. You will see that Defaulted and deleted functions isn't yet available.



回答2:

Visual Studio with the MSVC++ compiler does not support defaulted and deleted functions. You'll need to use something like MinGW's G++.



标签: c++ c++11