加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – gcc编译错误(关于复制c’tors)看起来很奇怪(至少对我而言)

发布时间:2020-12-16 07:28:52 所属栏目:百科 来源:网络整理
导读:所以,我有以下代码无法在OSX上的 gcc 4.2.1上编译.我得到的错误是: testref.cpp: In function ‘int main()’:testref.cpp:10: error: ‘A::A(const A)’ is privatetestref.cpp:20: error: within this context 这是代码 #include cstdioclass A {public:
所以,我有以下代码无法在OSX上的 gcc 4.2.1上编译.我得到的错误是:

testref.cpp: In function ‘int main()’:
testref.cpp:10: error: ‘A::A(const A&)’ is private
testref.cpp:20: error: within this context

这是代码

#include <cstdio>

class A {
public:
    A() { i=0; printf("A ctorn"); }
    ~A() { printf("A dtorn"); }

private:
    A(const A& other) { i=other.i; printf("A COPY CTORn"); }
    A& operator=(const A& other) { i=other.i; printf("A COPY operatorn"); return *this; }
private:
    int i;
};

void f(const A &aref) {
    printf("dummyn");
} 

int main() {
    f(A());
    return 0; 
}

在这种情况下不需要这个拷贝构造函数,因为f得到一个引用(我公开它是为了看它是否被调用而它没有).
另外,我已经使f按值获取对象,并且仍然既没有复制构造函数也没有调用operator =.我怀疑这可能与优化有关.
有什么建议?
谢谢.

解决方法

你陷入了一个微妙的标准问题. GCC是对的,但错误非常糟糕:用clang编译相同的内容给出:

test.cpp:20:7: warning: C++98 requires an accessible copy constructor for class
              'A' when binding a reference to a temporary; was private

编辑:我没有附近的标准副本给你完整的推理.希望其他人(或谷歌)可以.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读