1

I have the following Timer.cpp, Timer.h, and main.cpp files. I am trying to call functions from the Timer.cpp file in my main.cpp file and have included the Timer.h in the main, but it is still not working. Can someone please explain why? I am a little rusty with C++ and feel like I am making a silly mistake. Thanks in advance for any help.

#Timer.h file

#ifndef __Notes__Timer__
#define __Notes__Timer__

#include <iostream>

class Timer {
public:
    Timer();
    void start();
    void stop();
    void clear();
    float getDelta();
};

#endif

#Timer.cpp file    
#include "Timer.h"


clock_t startTime;
clock_t stopTime;

Timer::Timer(){
    startTime = 0;
    stopTime = 0;
}//Timer

void start(){
    startTime = clock();
}//start

void stop(){
    stopTime = clock();
}//stop

float getDelta(){
    return stopTime-startTime;
}//getDelta

#main.cpp file

#include "Timer.h"
#include <iostream>

using namespace std;


int main(){
    char quit;
    start();
    cout << "Would you like to quit? Y or N: ";
    cin >> quit;
    if (quit != 'Y' || quit != 'y'){
        while (quit != 'Y' || quit != 'y'){
            cout << "Would you like to quit? Y or N: ";
            cin >> quit;
        }//while
    }//if
    else {
        stop();
        cout << getDelta();
        exit(0);
    }//else
}//main
user1655752
  • 45
  • 1
  • 5

2 Answers2

0

you can use these functions in Timer.c and use this class::function

0

It seems the reason it is not working is because of two things: Your Timer function definitions are not tied to the Timer class. For example, the function definition for start in Timer.cpp needs to be

     void Timer::start()
     {
       //Do stuff
     }

All of the other functions in the Timer.cpp file need to follow that same syntax. In addition the startTime and stopTime variables should go in the Timer.h file like this:

class Timer
{
  private:
    clock_t startTime;
    clock_t stopTime;

  public:
    Timer();
    void start();
    //etc
}

Next, in the main function, you need to create a Timer object:

 int main()
 {
   char quit;
   Timer* timer_obj = new Timer();
   timer_obj -> start();
   //Do stuff
   timer_obj -> stop();
   cout << timer_obj -> getDelta() ;
   delete timer_obj ;
   //exit
 }
Justin H.
  • 143
  • 8
  • Wrong way to make objects in C++. `Timer timer_obj;`. – chris Apr 05 '13 at 03:39
  • Using Timer* timer_obj = new Timer() is just as valid as using Timer timer_obj. The first one simply creates a pointer to the object in memory. The advantage of the first one, is that you can pass object pointers around without having to copy the entire object every time you want to pass into a function. The drawback is making sure you properly delete the object when you are done with it. – Justin H. Apr 05 '13 at 04:12
  • Sure, if you want problems later on when you forget to `delete` it, or when you try doing that in a class and don't realize you have to implement additional members to make it work properly. Why make it harder than it has to be? – chris Apr 05 '13 at 04:14
  • Passing around pointers are a lot more efficient when you are concerned about performance. True, you have to deal with the issue of taking care of memory management, but in many cases in industry, the need for performance outweighs everything else. – Justin H. Apr 05 '13 at 04:26
  • You can usually pass around references in that case. If you absolutely need a pointer, use a smart pointer. – chris Apr 05 '13 at 04:28