嵌套类

时间:2015-05-19 08:34:00

标签: c++ pugixml

我有以下C ++代码:

#include <ctime>
#include <vector>
#include <pugixml/pugixml.hpp>          //pugixml (version 1.6)

using namespace pugi;

class ClassA
{
    public:
        xml_document doc;

        void writeString(const char *str)
        {
            doc.append_child("randomString").append_child(node_pcdata).set_value(str);
        }
};

class ClassB
{
    private:
        class ClassB_Child
        {
            public:
                ClassA aInstance;
                long timestamp;

            public:
                ClassB_Child()
                {
                    timestamp = time(NULL);
                    aInstance.writeString("Hello world!");
                }
        };

    public:
        std::vector<ClassB_Child> vecBChild;

        ClassB()
        {
            vecBChild.push_back(ClassB_Child());
        }
};

int main()
{
    ClassB bInstance;

    return 0;
}

但每次我想编译它(使用MinGW g ++ 4.8.1)时,只会产生很多错误:

In file included from broken.cpp:3:0:
broken.cpp: In instantiation of 'void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = ClassB::ClassB_Child; _Alloc = std::allocator<ClassB::ClassB_Child>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<ClassB::ClassB_Child*, std::vector<ClassB::ClassB_Child> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = ClassB::ClassB_Child*]':
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_vector.h:913:28:   required from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = ClassB::ClassB_Child; _Alloc = std::allocator<ClassB::ClassB_Child>; std::vector<_Tp, _Alloc>::value_type = ClassB::ClassB_Child]'
broken.cpp:40:38:   required from here
c:\mingw\include\pugixml\pugixml.hpp:941:3: error: 'pugi::xml_document::xml_document(const pugi::xml_document&)' is private
   xml_document(const xml_document&);
   ^
broken.cpp:7:7: error: within this context
 class ClassA
       ^
broken.cpp:21:9: note: synthesized method 'ClassA::ClassA(const ClassA&)' first required here 
   class ClassB_Child
         ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\vector:69:0,
                 from broken.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\vector.tcc:329:19: note: synthesized method 'ClassB::ClassB_Child::ClassB_Child(const ClassB::ClassB_Child&)' first required here 
    _Tp __x_copy = __x;
                   ^
In file included from broken.cpp:3:0:
c:\mingw\include\pugixml\pugixml.hpp:942:23: error: 'const pugi::xml_document& pugi::xml_document::operator=(const pugi::xml_document&)' is private
   const xml_document& operator=(const xml_document&);
                       ^
broken.cpp:7:7: error: within this context
 class ClassA
       ^
broken.cpp:21:9: note: synthesized method 'ClassA& ClassA::operator=(const ClassA&)' first required here 
   class ClassB_Child
         ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\vector:69:0,
                 from broken.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\vector.tcc:335:16: note: synthesized method 'ClassB::ClassB_Child& ClassB::ClassB_Child::operator=(const ClassB::ClassB_Child&)' first required here 
    *__position = __x_copy;
                ^

我还遇到了,当我删除pugi::xml_document doc;行时,所有错误都消失了(然后程序编译并运行正常)。这意味着,错误必须由pugixml引起。我需要在代码中更改什么才能让程序正确编译?

1 个答案:

答案 0 :(得分:0)

第一个错误解释了你的问题,虽然是加密的:

  

pugixml.hpp:在成员函数&#39; ClassA :: operator =(const ClassA&amp;)&#39;:

编译器正在尝试为您合成默认赋值运算符。默认实现将从源ClassA中的相应成员分配目标ClassA的每个成员。在这种情况下,它看起来好像

ClassA::operator=(const ClassA& other)
{
    doc = other.doc;
}

当它试图复制doc成员时,它发现不允许复制它:

  

pugixml.hpp:942:23:错误:&#39; const pugi :: xml_document&amp;   pugi :: xml_document :: operator =(const pugi :: xml_document&amp;)&#39;是私人的

无法复制doc意味着我们无法复制ClassA;反过来,这意味着我们无法复制ClassB_Child,而这反过来会阻止声明std::vector ClassB_Child

为了解决您的问题,您需要决定复制包含xml_document的类时要发生的事情。如果有提供正确类型的副本(可能是深拷贝)的方法,那么创建一个合适的赋值运算符(和相应的拷贝构造函数,可能还有一个析构函数) - 搜索&#34;三个规则&# 34;或甚至&#34;规则为5&#34;或者#34;规则为零&#34;在网上)。或者您可能希望共享一个XML文档?