c++ - 编译器不允许定义编译器生成的构造函数

标签 c++

我正在尝试定义编译器自动生成并导致编译错误的构造函数。这是我的代码:

class myclass
 {
   public:
       void Test_Func()
       {}
 };
myclass::myclass()
{
}

编译器是否为上面的简单类做任何额外的事情。这是我在 MSVC 编译器中遇到的错误:

"error C2600: 'myclass::myclass' : cannot define a compiler-generated special member function (must be declared in the class first)"

最佳答案

合成构造函数既被声明又被定义。您可能不会自己定义它。

您必须声明自己的构造函数才能提供实现。

[special](2003 年措辞,12/1)说:

The default constructor (12.1), copy constructor and copy assignment operator (12.8), and destructor (12.4) are special member functions. The implementation will implicitly declare these member functions for a class type when the program does not explicitly declare them, except as noted in 12.1. The implementation will implicitly define them if they are used, as specified in 12.1, 12.4 and 12.8. Programs shall not define implicitly-declared special member functions.

关于c++ - 编译器不允许定义编译器生成的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6322573/

相关文章:

c++ - 如何在没有 STL 的情况下从二维数组中删除任何未填充的插槽?

c++ - 在 C++ 代码中调用 WMIC 命令

C++ - 智能指针 - 通过模板将派生类共享指针传递给基类

c++ - 对非对象使用成员初始化列表不好吗?

c++ - 是否可以将新分配的 block 初始化为 0?

c++ - opengl 中的着色器应该做多少工作?

c++ - 阵列衰减和修改

c++ - 如何以字节为单位获取 UTF-16LE 字符串的大小?

c++ - 搜索二叉搜索树 - 基于数组

c++ - leetcode 中的二叉树层序遍历