Re: How can I initialize an array whose type is a struct?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

>How can I initialize an array whose type is a struct?

You aren't initializing an array, you are initializing a std::vector.

You cannot construct a std::vector that way, as you did in your code. You can construct arrays like that (with the appropriate syntax).

For a std::vector, use the push_back method.

Also, "iter->days" should be "iter->day".

HTH,
--Eljay

- - - - - - - - - - - - - - - - - - -

#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;

struct year
{
    year(string const& m, int d)
    : month(m), day(d)
    { }

    string month;
    int day;
};

int main()
{
    vector<year> arr;
    arr.push_back(year("Jan", 31));
    arr.push_back(year("Feb", 28));

    vector<year>::iterator iter;
    for(iter = arr.begin(); iter != arr.end(); ++iter)
        cout << iter->month << " " << iter->day << endl;
}


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux