C++ gcc和Visual Studio的不同编译错误,'within this context'
C++ Different compilation error with gcc and visual studio, 'within this context'
我以前贴过一个类似的问题,但我在代码中有一个小错误,所以它被忽略了,让我再问一次。
在Gcc中,我得到了编译错误的确切行在VS2010/2012我不知道编译错误在哪里,有人能帮助吗?**在VS中,我怎么知道哪一行调用了它?
我有以下代码:
#include "ObjectHolder.h"
int main()
{
ObjectHolder first(1);
ObjectHolder second(first);
return 0;
}
#ifndef NonCopyableObject_Included
#define NonCopyableObject_Included
class NonCopyableObject
{
public:
virtual ~NonCopyableObject () {}
NonCopyableObject(int i) { m_index = i;}
int m_index;
private:
NonCopyableObject(const NonCopyableObject& other) {}
};
#endif
#ifndef ObjectHolder_Included
#define ObjectHolder_Included
#include "NonCopyableObject.h"
class ObjectHolder
{
public:
virtual ~ObjectHolder ();
ObjectHolder(int i) : obj(i) {}
NonCopyableObject obj;
};
#endif
VS错误:1>d:usersuserdocumentsvisual studio 2012projectstestertesterobjectholder.h(13):
error C2248: 'NonCopyableObject::NonCopyableObject' : cannot access private member declared in class 'NonCopyableObject'
d:usersuserdocumentsvisual studio 2012projectstestertester
noncopyableobject.h(13) : see declaration of 'NonCopyableObject::NonCopyableObject'
d:usersuserdocumentsvisual studio 2012projectstestertester
noncopyableobject.h(6) : see declaration of 'NonCopyableObject'
This diagnostic occurred in the compiler generated function
'ObjectHolder::ObjectHolder(const ObjectHolder &)'
GCC:
$ g++ -Wall -Werror --std=c++0x main.cpp -o test_no_copy
In file included from main.cpp:2:0:
NonCopyableObject.h: In copy constructor `ObjectHolder::ObjectHolder(const ObjectHolder&)':
NonCopyableObject.h:13:3: error: `NonCopyableObject::NonCopyableObject(const NonCopyableObject&)' is private
ObjectHolder.h:7:1: error: within this context
main.cpp: In function `int main()':
main.cpp:8:27: note: synthesized method `ObjectHolder::ObjectHolder(const ObjectHolder&)' first required here
编译器不能在ObjectHolder
中生成(合成)复制构造函数,因为它包含一个具有私有复制构造函数的NonCopyableObject
类的对象。
默认情况下,生成的复制构造函数调用所有成员对象和父对象的复制构造函数。
使用不同的编译器进行编译是一个好主意,因为不同的编译器之间的错误差异很大,其中一个可能比另一个更有帮助。在大多数情况下,我发现Clang的错误报告是最好的。在您的示例中:
source.cpp:9:48: error: unused parameter 'other' [-Werror,-Wunused-parameter] NonCopyableObject(const NonCopyableObject& other) {} ^ source.cpp:25:18: error: call to implicitly-deleted copy constructor of 'ObjectHolder' ObjectHolder second(first); ^ ~~~~~ source.cpp:18:23: note: copy constructor of 'ObjectHolder' is implicitly deleted because field 'obj' has an inaccessible copy constructor NonCopyableObject obj;
您正在尝试复制ObjectHoder
ObjectHolder second(first);
但是它包含一个不可复制的对象,所以它不能被复制。您正在尝试执行代码在编译时被设计为不允许的操作。两个编译器都告诉你这个
相关文章:
- C++错误:"error: int aaa::bbb is protected within this context"
- "Warning: Comma within array index expression"但逗号分隔函数参数
- boost::geometry::within() 点和序列的行为
- c++ std::bind within function
- 使用外部依赖项运行 VC++ 单元测试时"Failed to set up execution context"
- 如何在Qt中调用context.getExternalFilesDir
- 'default-initialization in copy-initialization context'在C++中是什么意思?
- 如果 int 是"not within the enums range",为什么将 int 转换为强类型枚举会编译?
- 为什么不允许优化器折叠"constant context"?
- "C4649: attributes are ignored in this context"的含义是什么?
- boost::context::basic_segmented_stack 不是按需增长
- "Attribute is protected within this context"继承和 .h 和.cpp文件
- "Default member initializer needed within definition of enclosing class outside of member functions
- ZeroMQ 在 context.close() 中被阻止.如何在C++中安全地关闭套接字和上下文?
- 'context string for the given token'是什么意思?
- "is private within this context"被抛出用于不应是私有的函数(GCC 5.3.0,C++11)
- "error: within this context" 在Qt 5中安装QFtp
- 私有/受保护的变量"error: within this context"
- C++ gcc和Visual Studio的不同编译错误,'within this context'
- G++ 编译错误"... is protected from within this context"而 clang 没有错误