2014-08-29 32 views
0

編譯我的三個文件的程序(main.cpp中,source.cpp,header.hpp)產生這些錯誤:編譯器不會讓我在頭文件返回rapidxml :: xml_document,並報告錯誤

source.cpp: In member function ‘rapidxml::xml_document<> MyClass::generate_xml_document()’: 
source.cpp:559:9: error: use of deleted function ‘rapidxml::xml_document<>::xml_document(const rapidxml::xml_document<>&)’ 
In file included from header.hpp:12:0, 
       from source.cpp:11: 
rapidxml.hpp:1358:11: error: ‘rapidxml::xml_document<>::xml_document(const rapidxml::xml_document<>&)’ is implicitly deleted because the default definition would be ill-formed: 
rapidxml.hpp:1322:9: error: ‘rapidxml::xml_node<Ch>::xml_node(const rapidxml::xml_node<Ch>&) [with Ch = char, rapidxml::xml_node<Ch> = rapidxml::xml_node<char>]’ is private 

命名的線路有:

  • source.cpp:559個狀態根本return doc;。函數的結尾會生成rapidxml::xml_document<>
  • header.hpp:12和source.cpp:11狀態#include "rapidxml.hpp"
  • 周圍rapidxml.hpp面積:1322狀態:

    private: 
        // Restrictions 
    
        // No copying 
        xml_node(const xml_node &); 
        void operator =(const xml_node &); 
    
  • rapidxml.hpp:1358是類xml_document的開頭:class xml_document: public xml_node<Ch>, public memory_pool<Ch>

這是rapidxml錯了嗎? (我很確定它不是,因爲Marcin Kalicinski肯定比我更好的程序員)。

+1

我不知道rapidxml,但是說你不能複製它。你需要通過引用或指針傳遞它 – 2014-08-29 02:58:26

回答

2

基本上,RapidXML xml_document類型是不可複製的。正如您發佈的代碼段所顯示的(以及註釋「不允許複製」意味着),複製構造函數和賦值運算符是私有的,以強制編譯器錯誤。

您應該在函數中動態創建一個函數,然後返回一個指針 - 或者使該函數對現有的xml_document作爲輸入的引用。

所以不是這個

xml_document myFunc() 
{ 
    xml_document doc; 
    ... 
    return doc; 
} 

xml_document d = myfunc(); 

..you'll需要這個

void myFunc(xml_document &doc) 
{ 
    ... 
} 

xml_document d; 
myfunc(d); 

或者,使用動態分配:

xml_document *myFunc() 
{ 
    xml_document *doc = new xml_document(); 
    return doc; 
} 

xml_document d = myfunc(); 
... 
delete d; 

後者顯然需要智能指針,但這顯示了這個想法。

相關問題