首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套类中的pugixml错误

嵌套类中的pugixml错误
EN

Stack Overflow用户
提问于 2015-05-19 16:34:00
回答 1查看 478关注 0票数 0

我得到了以下C++代码:

代码语言:javascript
复制
#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)时,都会生成许多错误:

代码语言:javascript
复制
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引起。我必须在我的代码中进行哪些更改才能使程序正确编译?

EN

回答 1

Stack Overflow用户

发布于 2015-05-19 16:55:31

第一个错误解释了您的问题,尽管有点晦涩:

pugixml.hpp:在成员函数'ClassA::operator=(const ClassA&)‘中:

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

代码语言:javascript
复制
ClassA::operator=(const ClassA& other)
{
    doc = other.doc;
}

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

pugixml.hpp:942:23:错误:'const pugi::xml_document& pugi::xml_document::operator=(const pugi::xml_document&)‘是私有的

不能复制doc意味着我们不能复制ClassA;反过来,这又意味着我们不能复制ClassB_Child,这反过来又阻止了std::vector of ClassB_Child的声明。

为了解决您的问题,您需要确定在复制包含xml_document的类时希望发生什么。如果有一种方法可以提供正确类型的副本(可能是深度副本),那么创建一个适当的赋值操作符(以及相应的副本构造函数和可能的析构函数-在Web上搜索“三规则”、甚至“五规则”或“零规则”)。或者您可能想要共享单个XML文档?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30320541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档