May 12, 2012

annitaleo annitaleo
Lab Rat
9 posts

count the number of film instances

 

check this out:

Film.h

  1. #ifndef FILM_H
  2. #define FILM_H
  3. #include <QString>
  4.  
  5. class Film {
  6.   protected:
  7.     QString title;
  8.     double dailyRate;
  9.   public:
  10.     Film(QString ti,double dr);
  11.     virtual double calculateRental(int num) const;
  12. };
  13.  
  14. #endif // FILM_H

Film.cpp

  1. #include "film.h"
  2. #include <QString>
  3.  
  4. Film::Film(QString ti,double dr){
  5.   title=ti;
  6.   dailyRate=dr;
  7.  }
  8.  
  9.  double Film::calculateRental(int num) const {
  10.  return dailyRate*num;
  11. }

main.cpp
  1.  #include <QtCore/QCoreApplication>
  2.  #include <QtCore/QTextStream>
  3.  #include  "film.h"
  4.  
  5.  int main(int argc, char *argv[]) {
  6.  QCoreApplication a(argc, argv);
  7.  QTextStream cout(stdout, QIODevice::WriteOnly);
  8.  Film f("Top Gun", 10.00);
  9.  cout <<f.calculateRental(2);
  10.  
  11. return a.exec();
  12.  
  13. }

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

  1. static int numOfFilms;
  2. numOfFilms++;

9 replies

May 12, 2012

koahnig koahnig
Dinosaur Breeder
2908 posts

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

May 12, 2012

annitaleo annitaleo
Lab Rat
9 posts

typing error

May 12, 2012

koahnig koahnig
Dinosaur Breeder
2908 posts

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.

May 12, 2012

favoritas37 favoritas37
Lab Rat
148 posts

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

  1.      
  2.     #ifndef FILM_H
  3.     #define FILM_H
  4.     #include <QString>
  5.      
  6.     class Film {
  7.       protected:
  8.         QString title;
  9.         double dailyRate;
  10.       public:
  11.         Film(QString ti,double dr);
  12.         virtual double calculateRental(int num);
  13.     };
  14.      
  15.     #endif // FILM_H

Film.cpp

  1.      
  2.     #include "film.h"
  3.     #include <QString>
  4.      
  5.     Film::Film(QString ti,double dr){
  6.       title=ti;
  7.       dailyRate=dr;
  8.      }
  9.      
  10.      double Film::calculateRental(int num) {
  11.      return dailyRate*num;
  12.     }

main.cpp

  1.      
  2.      #include <QtCore/QCoreApplication>
  3.      #include <QtCore/QTextStream>
  4.      #include  "film.h"
  5.  
  6.      using namespace std;
  7.      
  8.      int main(int argc, char *argv[]) {
  9.      QCoreApplication a(argc, argv);
  10.      QTextStream cout(stdout, QIODevice::WriteOnly);
  11.      Film f("Top Gun", 10.00);
  12.      cout <<f.calculateRental(2);
  13.      
  14.     return a.exec();
  15.      
  16.     }

May 12, 2012

annitaleo annitaleo
Lab Rat
9 posts

OS: windows 7
compiler QT4

May 12, 2012

favoritas37 favoritas37
Lab Rat
148 posts

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

May 12, 2012

annitaleo annitaleo
Lab Rat
9 posts

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

May 12, 2012

koahnig koahnig
Dinosaur Breeder
2908 posts

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

  1. double Film::calculateRental(int num)
  2. {
  3.      return dailyRate*double(num);
  4. }

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.

May 12, 2012

Paker Paker
Lab Rat
10 posts

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.

 Signature 

Carlos C├ęsar Neves Enumo
Computer Science Student
UNESP - Universade Estadual Paulista

 
  ‹‹ How to create Qt apps (VS2008) ?      [SOLVED]QtCore4.dll missing entry point ››

You must log in to post a reply. Not a member yet? Register here!