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

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

概述所以,我有以下代码无法在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 <cstdio>cla 所以,我有以下代码无法在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 <cstdio>class A {public:    A() { i=0; printf("A ctor\n"); }    ~A() { printf("A dtor\n"); }private:    A(const A& other) { i=other.i; printf("A copY CTOR\n"); }    A& operator=(const A& other) { i=other.i; printf("A copY operator\n"); return *this; }private:    int i;};voID f(const A &aref) {    printf("dummy\n");} 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

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

总结

以上是内存溢出为你收集整理的c – gcc编译错误(关于复制c’tors)看起来很奇怪(至少对我而言)全部内容,希望文章能够帮你解决c – gcc编译错误(关于复制c’tors)看起来很奇怪(至少对我而言)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1229907.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存