我正在尝试进行字符串解析(事实证明这是一个巨大的痛苦)。我收到此错误:“聚合对象应使用 '{...}' 进行初始化”
我有一个这样定义的元素:
Element * currentBar = new Element("Bar");
我想制作一个数组或其他东西来存储多个条形图,所以我正在尝试做这样的事情:
Element allBars [] = new Element("Bars");
我很确定这不是我想要做的,特别是因为我收到这个错误“初始化为聚合对象需要'{...}'”
这是我的一段代码:
if(!chartDataString.empty()){
chartData.clear();
int index = 0;
char c, c1, c2;
inputMode currentInputMode = inputMode::UNKNOWN;
Element * cur = NULL;
while(index<chartDataString.size()){
c = chartDataString[index];
c1 = chartDataString[index+1];
c2 = chartDataString[index+2];
string s;
s = c1;
Element * currentBar = new Element("Bar");
Element allBars [] = new Element("Bars");
if(c == '*'){
if(c1 == 'i'){
currentBar->addChild(Element("rehearsalLetter", "info"));
}
else{
currentBar->addChild(Element("leftDoubleBarLine", s));
index++;
}
else if(c == '['){
currentBar->addChild(Element("leftDoubleBarLine"));
}
else if(c == 'T'){
string signature = string() + c1 + '/' + c2;
currentBar->addChild(Element("timeSignature", signature));
index += 2;
}
//start a new bar
else if(c == '|'){
allBars->addChild(currentBar);
currentBar = new Element("bar");
}
我的元素类以防万一它有帮助:
#include <string>
#include <ostream>
#include "ArrayList.h"
class Element{
public:
Element(){}
Element( const string& _title ){
title = _title;
}
Element( const string& _title, const string& _value){
title = _title;
value = _value;
};
~Element(){};
void addChild(Element & child){
children.add(child);
}
void serialize(ostream & o ){
if( children.size() == 0 ){
o << "<" << title << ">";
o << " " << value << " ";
o << "</" << title << ">";
}else{
o << "<" << title << ">" << endl;
for( int i = 0; i < children.size(); ++i ){
children.elementAt(i).serialize(o);
}
o << "</" << title << ">" << endl;
}
}
private:
string title;
string value;
ArrayList<Element> children;
};