0

我之前发布了一个类似的问题,但我在代码中有一个小错误,所以它被忽略了,让我再问一次。

在 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:\users\user\documents\visual studio 2012\projects\tester\tester\objectholder.h(13): 

     error C2248: 'NonCopyableObject::NonCopyableObject' : cannot access private member declared in   class 'NonCopyableObject'
               d:\users\user\documents\visual studio 2012\projects\tester\tester  
     \noncopyableobject.h(13) : see declaration of 'NonCopyableObject::NonCopyableObject'
            d:\users\user\documents\visual studio 2012\projects\tester\tester
     \noncopyableobject.h(6) : see declaration of 'NonCopyableObject'
            This diagnostic occurred in the compiler generated function    
     'ObjectHolder::ObjectHolder(const ObjectHolder &)'

海合会:

$ 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
4

3 回答 3

1

The compiler cannot generate (synthesize) a copy-constructor in ObjectHolder because it contains an object of class NonCopyableObject which has a private copy-contructor.

By default the generated copy-constructor is invoking the copy-constructor of all member- and parent objects.

于 2013-03-12T11:22:48.810 回答
1

使用不同的编译器进行编译是一个好主意,因为它们之间的错误可能会有很大差异,其中一个可能比另一个更有帮助。在大多数情况下,我发现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;

在这里看到它

于 2013-03-12T11:32:27.227 回答
1

您正在尝试复制ObjectHoder

ObjectHolder second(first);

但是它包含一个不可复制的对象,所以它不能被复制。您正在尝试执行您的代码在编译时不允许执行的操作。两个编译器都在告诉你这一点。

于 2013-03-12T11:22:26.683 回答