Count the number of film instances



  • check this out:

    Film.h
    @
    #ifndef FILM_H
    #define FILM_H
    #include <QString>

    class Film {
    protected:
    QString title;
    double dailyRate;
    public:
    Film(QString ti,double dr);
    virtual double calculateRental(int num) const;
    };

    #endif // FILM_H
    @

    Film.cpp
    @
    #include "film.h"
    #include <QString>

    Film::Film(QString ti,double dr){
    title=ti;
    dailyRate=dr;
    }

    double Film::calculateRental(int num) const {
    return dailyRate*num;
    }
    @
    main.cpp
    @
    #include <QtCore/QCoreApplication>
    #include <QtCore/QTextStream>
    #include "film.h"

    int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
    QTextStream cout(stdout, QIODevice::WriteOnly);
    Film f("Top Gun", 10.00);
    cout <<f.calculateRental(2);

    return a.exec();

    }@

    how do I count the number of Film instances created? I know is something like that:

    @static int numOfFilms;
    numOfFilms++;@


  • Moderators

    The at sign in line 5 of Film.cpp is that just a typing error in the post?



  • typing error


  • Moderators

    I have corrected the code wrappings of your initial post.

    You need to post the error messages as they come from the compiler. Typically there is an indication of where the error is detected. Also some additional information of your system (OS, compiler, IDE) may be of advantage.



  • Two things that i would recommend. First at the closing bracket of each class you have to put a ';'. Second i don't see a "using namespace std;". I suppose you already have it somewhere else but...So your code looks like this now:

    Film.h
    @
    #ifndef FILM_H
    #define FILM_H
    #include <QString>

    class Film {
      protected:
        QString title;
        double dailyRate;
      public:
        Film(QString ti,double dr);
        virtual double calculateRental(int num);
    };
     
    #endif // FILM_H
    

    @

    Film.cpp
    @
    #include "film.h"
    #include <QString>

    Film::Film(QString ti,double dr){
      title=ti;
      dailyRate=dr;
     }
     
     double Film::calculateRental(int num) {
     return dailyRate*num;
    }
    

    @

    main.cpp
    @
    #include <QtCore/QCoreApplication>
    #include <QtCore/QTextStream>
    #include "film.h"

     using namespace std;
     
     int main(int argc, char *argv[]) {
     QCoreApplication a(argc, argv);
     QTextStream cout(stdout, QIODevice::WriteOnly);
     Film f("Top Gun", 10.00);
     cout <<f.calculateRental(2);
     
    return a.exec&#40;&#41;;
     
    }
    

    @



  • OS: windows 7
    compiler QT4



  • Once again, place the semicolon after the Film class definition.



  • inclusion of the statement dailyRate=10.00 in the calculateRental() function produces a compiler error. Is it because dailyRate is declared as double?


  • Moderators

    Qt4 is not a compiler at all. This is the Qt library. Did you install Qt SDK? Qt creator? Anyway, both are using Mingw compiler as standard on windows.

    try this
    @
    double Film::calculateRental(int num)
    {
    return dailyRate*double(num);
    }
    @

    This makes sure that the return will be double. However, it should not be an error reported by the compiler.

    Therefore, it would be good to post also the complete compile output. Otherwise this is quite a guess work, which may frustrate you as well as the others trying to help you.

    Furthermore, you may want to consider doing some C++ tutorial. This will help you to get a better understanding of C++ and the use of compilers and the interpretation of the error messages.



  • Man... your errors are all because of the ; missing after class declaration in Film.h.
    The "two or more data types in declaration of 'main'" is like you are declaring your main as:

    Filme int main(int argc, char *argv[])

    If you put the ; after class declaration all errors disappear.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.