我在爲我的孩子們做的一個項目上有一個奇怪的行爲。我想用rapidXml解析xml,並在RasPi1B上用低音播放mp3。一切正常,如果我只是在一個主要功能中按下它。但只要我把代碼放到另一個類中,我的xml數據就會被破壞。 我只在我的私人時間做C++,並且使用託管語言,所以我沒有將它變成C++中的內存管理。所以我希望你們能幫助我(和我的孩子)擺脫這個問題。當然,我可以在短短的一類求所有,但大腦會試圖嗆死;-)Raspberry Pi上的C++ bass和rapidXml 1 B
首先在一個單一的主要功能:
// test.cpp
#include <iostream>
#include <rapidxml.hpp>
#include <rapidxml_utils.hpp>
#include <rapidxml_print.hpp>
#include <bass.h>
using namespace std;
using namespace rapidxml;
int main(int argc, char **argv)
{
file<> xmlFile("bin/test.xml");
xml_document<> doc;
doc.parse<0>(xmlFile.data());
print(std::cout, doc, 0); // everything is fine
bool init = BASS_Init(-1, 44100, 0, 0, NULL);
cout << "BASS_Init " << (init ? "true" : "false") << " [error: " << BASS_ErrorGetCode() << "]" << endl;
print(std::cout, doc, 0); // everything is fine
return 0;
}
然後(幾乎)與相同主要功能和類/頭:
//test2.cpp
#include <iostream>
#include "player.hpp"
using namespace std;
using namespace player;
int main(int argc, char **argv)
{
Player* _pPlayer = new Player();
_pPlayer->loadXml();
_pPlayer->printXml();
_pPlayer->initPlayer();
_pPlayer->printXml();
return 0;
}
// player.cpp
#include "player.hpp"
using namespace std;
using namespace rapidxml;
namespace player {
void Player::loadXml(void)
{
file<> xmlFile("bin/test.xml");
doc.parse<0>(xmlFile.data());
}
void Player::printXml(void)
{
print(std::cout, doc, 0);
}
void Player::initPlayer(void)
{
bool init = BASS_Init(-1, 44100, 0, 0, NULL);
cout << "BASS_Init " << (init ? "true" : "false") << " [error: " << BASS_ErrorGetCode() << "]" << endl;
}
}
// player.hpp
#include <iostream>
#include <rapidxml.hpp>
#include <rapidxml_utils.hpp>
#include <rapidxml_print.hpp>
#include <bass.h>
using namespace std;
using namespace rapidxml;
namespace player {
class Player
{
public:
void loadXml(void);
void printXml(void);
void initPlayer(void);
private:
xml_document<> doc;
};
}
與編譯:
BASS_PATH = ../libraries/bass_arm
BASS_INCLUDE_PATH = -I$(BASS_PATH)
BASS_LIBRARY_PATH = -L$(BASS_PATH) -L$(BASS_PATH)/hardfp
BASS_LIBS = -lbass -lm
BASS_LINKER_FLAGS = -Wl,-rpath,$(BASS_PATH):$(BASS_PATH)/hardfp
RAPIDXML_PATH = ../libraries/rapidxml-1.13
RAPIDXML_INCLUDE_PATH = -I$(RAPIDXML_PATH)
RAPIDXML_LIBRARY_PATH = -L$(RAPIDXML_PATH)
test:
g++ -std=c++11 src_test/test.cpp -o bin/test $(BASS_INCLUDE_PATH) $(RAPIDXML_INCLUDE_PATH) $(BASS_LIBRARY_PATH) $(RAPIDXML_LIBRARY_PATH) $(BASS_LIBS) $(BASS_LINKER_FLAGS)
test2:
g++ -std=c++11 -g -c src_test/test2.cpp -o src_test/test2.o $(BASS_INCLUDE_PATH) $(RAPIDXML_INCLUDE_PATH)
g++ -std=c++11 -g -c src_test/player.cpp -o src_test/player.o $(BASS_INCLUDE_PATH) $(RAPIDXML_INCLUDE_PATH)
g++ -std=c++11 -g src_test/test2.o src_test/player.o -o bin/test2 $(BASS_LIBRARY_PATH) $(RAPIDXML_LIBRARY_PATH) $(BASS_LIBS) $(BASS_LINKER_FLAGS)
我的test.xml看起來如T他:
<?xml version="1.0" encoding="utf-8"?>
<library>
<series>
<title>Lorem ipsum A</title>
<episode>
<title>dolor sit amet 1</title>
<cover></cover>
<files rfid="x1" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_11.mp3</file>
<file>consetetur/sadipscing_elitr_12.mp3</file>
<file>consetetur/sadipscing_elitr_13.mp3</file>
<file>consetetur/sadipscing_elitr_14.mp3</file>
</files>
</episode>
<episode>
<title>dolor sit amet 2</title>
<cover></cover>
<files rfid="x2" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_21.mp3</file>
<file>consetetur/sadipscing_elitr_22.mp3</file>
<file>consetetur/sadipscing_elitr_23.mp3</file>
<file>consetetur/sadipscing_elitr_24.mp3</file>
</files>
</episode>
</series>
</library>
這裏是須藤箱的輸出/測試
<library>
<series>
<title>Lorem ipsum A</title>
<episode>
<title>dolor sit amet 1</title>
<cover/>
<files rfid="x1" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_11.mp3</file>
<file>consetetur/sadipscing_elitr_12.mp3</file>
<file>consetetur/sadipscing_elitr_13.mp3</file>
<file>consetetur/sadipscing_elitr_14.mp3</file>
</files>
</episode>
<episode>
<title>dolor sit amet 2</title>
<cover/>
<files rfid="x2" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_21.mp3</file>
<file>consetetur/sadipscing_elitr_22.mp3</file>
<file>consetetur/sadipscing_elitr_23.mp3</file>
<file>consetetur/sadipscing_elitr_24.mp3</file>
</files>
</episode>
</series>
</library>
BASS_Init true [error: 0]
<library>
<series>
<title>Lorem ipsum A</title>
<episode>
<title>dolor sit amet 1</title>
<cover/>
<files rfid="x1" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_11.mp3</file>
<file>consetetur/sadipscing_elitr_12.mp3</file>
<file>consetetur/sadipscing_elitr_13.mp3</file>
<file>consetetur/sadipscing_elitr_14.mp3</file>
</files>
</episode>
<episode>
<title>dolor sit amet 2</title>
<cover/>
<files rfid="x2" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_21.mp3</file>
<file>consetetur/sadipscing_elitr_22.mp3</file>
<file>consetetur/sadipscing_elitr_23.mp3</file>
<file>consetetur/sadipscing_elitr_24.mp3</file>
</files>
</episode>
</series>
</library>
而且須藤斌/ test2的的輸出:
<library>
<series>
<title>Lorem ipsum A</title>
<episode>
<title>dolor sit amet 1</title>
<cover/>
<files rfid="x1" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_11.mp3</file>
<file>consetetur/sadipscing_elitr_12.mp3</file>
<file>consetetur/sadipscing_elitr_13.mp3</file>
<file>consetetur/sadipscing_elitr_14.mp3</file>
</files>
</episode>
<episode>
<title>dolor sit amet 2</title>
<cover/>
<files rfid="x2" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_21.mp3</file>
<file>consetetur/sadipscing_elitr_22.mp3</file>
<file>consetetur/sadipscing_elitr_23.mp3</file>
<file>consetetur/sadipscing_elitr_24.mp3</file>
</files>
</episode>
</series>
</library>
BASS_Init true [error: 0]
<l1▒B>
CJ>
<>▒CJ▒BJ▒BJ</>
<device>
<de>aults.pcm.device</de>
<`CJ/>
<8CJ BJ="" iec958="1" 1▒CJ="">
<>▒DJ▒BJ6Jcardp3</>
<ults>pcm.cardr/1▒CJ▒CJ</ults>
<▒CJ>devicesg_edefault</▒CJ>
<1>DJPDJ▒CJ(DJ▒CJ</1>
</8CJ>
</device>
<EJpE>
<▒>J▒CJ6J</▒>
<cover/>
<cm.ca did="" J▒DJ="" pEJ="J">
<>devicesdipdefaults.pcm.d</>
<>@EJ▒DJEJ▒DJ</>
<rmat>rawur/sadip1▒EJ▒EJ</rmat>
<▒DJ>6Jtrueltstr_fi</▒DJ>
</cm.ca>
</EJpE>
<CJ>
</l1▒B>
對不起長髮布!我希望有一個人可以幫助我。我覺得真的很失落...... 在此先感謝!
噢,你讓我很快樂!我一直認爲xmlFile在doc.parse()之後沒有被使用...... 即使這個rapidxml_utils(定義了文件<>也給了我很多難以理解的分鐘,因爲我不知道如何處理整個模板的東西(不在模板中,但看起來有點像Java中的泛型)。 我通過向** .hpp **和'pXmlFile = new file <>(「bin/test.xml」);'**。cpp **添加'file <> * pXmlFile;'來解決它。 到目前爲止工作正常,但它是正確的方式? (編輯:差格式化 - 註釋中的選項^^) – FatalDiskError
另外作爲補充:是的,在我的「生產代碼」中,我使用'delete'。我只是想保持儘可能短的例子。 – FatalDiskError
使用指向'file <> *'的指針**是**正確的方式。另一種方法可能是沒有指針,使用'Player'的構造函數來初始化'file <> xmlFile'。在'Player'類的'public:'之後,你可以添加:'Player():xmlFile(「bin/test.xml」){}' – KompjoeFriek