why cant I acces private/protected members

I created a simple timer program but my method cant access the private members, anyone has any idea why.
and the answer is like 3 billion. did i do anythin wrong with the difftieme() funciton.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

class Timer
{
public:
	Timer()
	{
		start = false;
	}
	~Timer()
	{
		//delete StartTime;
		//delete EndTime;
	}
	void StartTimer()
	{		
		time(&StartTime);
	}

	void EndTimer()
	{
		time(&EndTime);
		ElapsedTime = difftime (EndTime, StartTime);
		
	}
	double getElapsed()
	{
		return ElapsedTime;
	}

		

private:

	time_t StartTime;
	time_t EndTime;
	double ElapsedTime;
	bool start;
};
int main()
{
	Timer *now = new Timer;
	char sentence[90];
	now->StartTimer;
	cout << "how fast could you type: \n";
	
	cin >> sentence;
	now->EndTimer();
        double ans = now->ElapsedTimer(); 
	cout << ans << "\n";
	system("pause");
	return 0;
}
Last edited on
Ok dont worry typo, I used the private varible instead of method, because they had almost the same name.
Please post the solution in case someone else have a similar problem:

Change line 47:
 
now->StartTime;

to
 
now->StartTimer();
Oh yeah I did it, only one problem, Difftime never returns a fraction, it always return a whole number(integer, even though it is double). any ideas.
According to the reference at this site, about time_t: "It is almost universally expected to be an integral value representing the number of seconds elapsed since [...]".

This means that your timer can only get the number of seconds.
Ok thanks, a bunch for your help, one last Q, is there any time value that is a float and not integer, I am gonna search the internet to see if now I guess I gotta use someone else codes (which I dont like because it just doesnt feel right).

EDIT:
I read up on the time() function return value and it states it returns what ever the argument is, only problem is the argument is time_t, is the way to make time_t a float, I gotta do more searching.
Last edited on
I only know about platform-specific functions. On unix you have gettimeofday(), and I'm sure there is something similar on windows.
Also, clock_gettime() is a POSIX function that will (usually) return fine-grained time values, but I don't know of the POSIX compliance of Windows.
jsmith wrote:
> Also, clock_gettime() is a POSIX function that will (usually) return
> fine-grained time values, but I don't know of the POSIX compliance
> of Windows.

It isn't very POSIX-compliant at all... Many of the POSIX functions that are used commonly on UNIX and its various derivatives are completely unavailable in Windows. I have yet to figure out why Microsoft has never even attempted to be POSIX-compliant.
Topic archived. No new replies allowed.