c++ - 聚合对象预期使用 '{...}' 进行初始化

标签 c++ string parsing initialization

我正在尝试进行字符串解析(事实证明这是一个巨大的痛苦)。我收到此错误:“聚合对象应使用 '{...}' 进行初始化”

我有一个这样定义的元素:

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;

};

最佳答案

当你声明一个变量时 Element allBars []编译器确实期望像 { Element("Bar"), Element("Foo") } 这样的值列表.这是一个静态数组,其大小在编译时已知。例如:

Element allBars [] = { Element("Bar"), Element("Foo") };

(请注意某些编译器确实需要在 [] 中指定元素的数量)。

如果你想声明一个动态数组,要么使用 std::vector<Element> allBars; ,我强烈推荐,因为它会导致增长和收缩,而无需担心内存分配和释放。您的类应该有一个默认构造函数、一个复制构造函数和一个赋值运算符(即使是由编译器生成的)。

或者您使用一个用new Element[xxx] 填充的指针喜欢Element* allBars = new Element[size_of_array]; ,它既不能增长也不能收缩,你必须使用 delete[] 显式删除为了调用Element数组中每个元素的析构函数。

在这两种情况下,每个 Element将使用默认构造函数初始化数组。

关于c++ - 聚合对象预期使用 '{...}' 进行初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454541/

相关文章:

c++ - 关于 Protocol Buffer 事件的设计问题

java - Java 中的缓冲读取器、行计数、拆分、解析

bash - 在 bash 中解析一行

C++ std::cout 不在字符串中打印\n

c++ - 如何改进我的哈希算法

java - 填写 "secret word"字母

android - Kotlin "Do not concatenate text displayed with setText. Use resource string with placeholders. "

php - 使用简单 Html dom 解析器进行 Html 解析

java - Java 泛型与 C++ 模板有何不同?为什么我不能使用 int 作为参数?

将所有小写字符转换为大写字符,反之亦然