Instantiation and constructors in c++

Pages: 12
Are you sure show[i] is properly initialized?
0/0/0
implies your operator << is working, but the date has not been set. Note that your default constructor for Date initializes month/day year to 0/0/0.

I'd look at your operator >> functions to ensure they're operating correctly.



Yeah, managed to make it work. Thanks.

I'm trying to figure out the = operator overload. I want to have a string earliestShow; and iterate through the vector, find the earliest show and assign the show[i].getDate = earliestShow. Any ideas?
Last edited on
You're going to want a findEarliestShow() function. That function should return a reference to a show. This really belogs in a Shows class, but since you don't have one yet, this can either be a global function, or a friend function inside your Show class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
friend Show & findEarliestShow()     .// Member of Show class
{  vector<Show>::iterator iter;
    vector<Show>::iterator earliest;
    if (shows.empty())
      throw std::exception ("No shows in vector");
    earliest = shows.begin();
    for (iter=shows.begin(); iter=shows.end(); iter++)
    {  if (shows[i].date < earliest->date)  // Requires Date overload the < operator
            earliest = iter; 
    }
    return *iter;
}

bool Date::operator < (const Date & rhs) const
{  return (year < rhs.year && month < rhs.month day < rhs.day);
}


assign the show[i].getDate = earliestShow.

getDate is a function. You can't put that on the left side of an assignment.

edit: Provided a more compact form of Date::operator <.
Last edited on
Cool. Thanks!
Topic archived. No new replies allowed.
Pages: 12