c++ - 错误 : base class 'A1' has private copy constructor

标签 c++ c++11 clang copy-constructor

在windows平台上使用Clang 3.7

见以下代码:

class A1
{
public:
    A1(char* name){}
    virtual ~A1() {}
private:
    A1(const A1&) {}
};

class B1 : public A1
{
public:
    B1(): A1(""){}
};

我收到以下错误:

 MyFile(31): 8: error: base class 'A1' has private copy constructor
         B1(): A1(""){}
               ^
 MyFile(25): 2: note: declared private here
         A1(const A1&) {}
         ^

公开 A1 复制构造函数,消除错误!

这里发生了什么?

注意:通过改变(我应该)

A1(const char* name)

我没有得到任何错误并且所有编译都按预期进行

最佳答案

我想这只是诊断信息生成方式的人工产物。

  • 首先,查找会尝试找到一个构造函数来匹配您的“调用”(这不是调用,而是其他)
  • 如您所知,使用 char* 的构造函数不匹配
  • 唯一的其他候选人是 private
  • 编译器会尝试查看它是否可以从您的 "" 参数实例化一个临时的 A1 以使其工作
  • 在这样做的过程中,它再次能找到的是private 构造函数
  • 编译器决定在做任何其他事情之前先提示一下

有人可能认为这是实现质量问题。

有趣的是,GCC 6.1.0 (even in pedantic C++14 mode) compiles your code as originally written , 仅针对损坏的文字转换发出警告。

关于c++ - 错误 : base class 'A1' has private copy constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37988456/

相关文章:

C++ 现代字符串指针

c++ - C++ 中的并行赋值

c++ - 停止与推力一起使用的 odeint 集成

c++ - 将类型转换为整数的元函数,反之亦然

c++ - 在启用 C++ 的 OSX 10.9 上编译 GMP 库

python - 用 libclang 解析;无法解析某些标记(Windows 中的 Python)

ios - iOS 单元/应用程序测试目标如何与其宿主应用程序中的类进行链接?

c++ - Openmp:无法正确计算并行 for 循环内的作业状态

C++设计: Passing pointer/reference to ref-counted object

c++ - 为什么从 C++11 中删除了对范围访问?