views:

69

answers:

3

in c++ ..

first of all I need to make a constructor of class Date d() that creates the current date.. what should i do?

after that i have a problem with this program:

class Noleggio
{
 public:
  //Noleggio(unsigned f, unsigned n, unsigned c) : inizio() { film = f; copia = n; cliente = c; restituito = false; }
  bool restituito;
  unsigned addebito;
  //unsigned film, copia, cliente;
  Data inizio;
  Data restituzione;
  CopiaFilm* copia;
  Film* film;
  Cliente* cliente;
};

class VideoNoleggio
{
 public:
  VideoNoleggio (string n) : nome(n) {}
  void InserisciFilm (Film* f);
  void InserisciCliente (Cliente* c);
  void CreaCopiaFilm (string s, Film* f);
  void NoleggioCopia (unsigned n, Film* f, Cliente* c);
  void RestituzioneCopia (unsigned n, Film* f, unsigned t);
  int CercaFilm (Film* f) const;
  int CercaCliente (Cliente* c) const;
  int CercaUltimaCopia(Film* f) const;
  int CercaNoleggio (unsigned n, Film* f) const;
  string Nome() const { return nome; }
  unsigned NumeroFilm() const { return film.size(); }
  unsigned NumeroNoleggi() const { return noleggi.size(); }
  Film* QualeFilm (unsigned i) const { return noleggi[i].film; }
  string QualeTitolo (unsigned i) const { return film[i]->Titolo(); }
 private:
  string nome;

  vector<Noleggio> noleggi;
  vector<CopiaFilm> copie;
  vector<Film*> film;
  vector<Cliente*> clienti;
};

in function

void VideoNoleggio::RestituzioneCopia (unsigned n, Film* f, unsigned t)
{
 int i = CercaUltimaCopia(f);
 assert (copie[i].numero >= n  );
 assert ( !(noleggi[i].restituito) );
 Data d();
 int j = CercaNoleggio(n,f);
 assert ( d >= noleggi[j].inizio );
 noleggi[j].restituzione = d;
 noleggi[j].restituito = true;
 noleggi[j].addebito = t*( d - noleggi[j].inizio + 1 );
 // clienti[noleggi[j]->cliente]->Prelievo(noleggi[j]->addebito);
}

the compiler returns an error here assert ( d >= noleggi[j].inizio ) it seems to don't like noleggi[j].inizio as a good parameter... (cause if i try d >= d it works---> it's not an operator overloading problem i guess)

in data.h friend bool operator >= (const Data& d1, const Data& d2);

any suggestion?

+3  A: 

My suggestion would be to use boosts date time library

daramarak
+6  A: 

The problem is the line

Data d();

This is interpreted as a function declaration (i.e. a function called d, taking no arguments and returning a Data).

Change it to

Data d;

or, if it's a POD structure and you want it value-initialised

Data d = Data();

As for how to implement the constructor, that will depend on your environment; there is no standard C++ date/time library. The most portable way is probably to use Boost.Date_Time, as daramarak suggests, but I've not used it myself so I can't do more than point you at the documentation. On Posix systems, you have the time() function to return the number of seconds since a defined date, and ctime() and related functions to break it down into years, months, etc. Windows probably has something similar, but I don't know about that.

Mike Seymour
ok that works! but what if i want Data d to be the current date? how do i have to define the constructor?
trippy
+2  A: 

Data d; is the declaration of a Data object called d.

Data d(); declares a function d() which returns a Data.

See C++ FAQ 10.2.

Dave