initialization with '{...}' expected for aggregate object

11,193

When you declare a variable Element allBars [] the compiler does expect a list of values like { Element("Bar"), Element("Foo") }. This is a static array whose size is known at compile time. So for example:

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

(Beware some compiler do require the number of element to be specified in the []).

If you want to declare a dynamic array, either you use a std::vector<Element> allBars;, which I would strongly recommend, because it cause grow and shrink without you worrying for memory allocation and deallocation. Your class should have a default constructor, a copy constructor and an assignment operator (even if generated by the compiler).

Or you use a pointer that you fill with new Element[xxx] like Element* allBars = new Element[size_of_array];, that cannot grow nor shrink and that you will have to explicitly delete using delete[] in order to call the Element destructor for every element of your array.

In both case, each Elementof the array will be initialized using the default constructor.

Share:
11,193

Related videos on Youtube

Sarah
Author by

Sarah

Previous Neuroscience student now CompSci student. Trying my best, but making too many stupid mistakes.

Updated on June 17, 2022

Comments

  • Sarah
    Sarah almost 2 years

    I am trying to do string parsing (and it is proving to be a huge pain). I'm getting this error: "initialization with '{...}' expected for aggregate object "

    I have an element defined like this:

    Element * currentBar = new Element("Bar");
    

    I want to make an array or something to store multiple bars, so I am trying to do something like this:

    Element allBars [] = new Element("Bars");
    

    I am pretty sure this is not what I want to do, especially since I am getting this error "initialization with '{...}' expected for aggregate object "

    This is a segment of my code:

    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");
            }
    

    And my element class just in case it's helpful:

    #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;
    
    };
    
    • Kerrek SB
      Kerrek SB over 10 years
      Try std::vector<Element>. Dynamic arrays are a wart and a cancer that doesn't fit into the language. There are multiple defect reports open concerning dynamic arrays.
  • Sarah
    Sarah over 10 years
    I have an array list class, it is possible just to make an array list of the bars?
  • Lightness Races in Orbit
    Lightness Races in Orbit over 10 years
    (Beware some compiler do require the number of element to be specified in the []). For example? Such compilers would be non-conformant.
  • Johan
    Johan over 10 years
    As I do not see the ArrayList code, I do not know :) But vector is great. Hard to beat in term of safety and performance for an array. The only problem is when you have a huge number of value that you can afford to store contiguously.
  • Lightness Races in Orbit
    Lightness Races in Orbit over 10 years
    @Johan: That's when you switch to std::deque
  • Sarah
    Sarah over 10 years
    Will doing this allow me to call addChild on allBars?
  • Johan
    Johan over 10 years
    @LightnessRacesinOrbit Yep, good advice ;) And for the compiler, I think it is just an old version of a compiler that rise an error in one previous question. Have to check what it was.
  • Johan
    Johan over 10 years
    @Sarah for (auto& elem: allBars) { elem.addChild(the_child); } if that suits you