Lost at storing classes in different files

Ok, this is one thing which since i started using C++, ive wanted to do, but spend all my time trying to get to work that i never actually program anything.

Basically, i want to store functions and classes in different files, accordingly.
Now i understand how header files etc work. However i can never seem to get past Mr. Linker.

So in this scenario i want to store a class in another file, which has functions. For organization and for OOP.

So heres the code i have so far:

IG_Timers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once

typedef enum { _Loop, _Move, _PosAI, _IPS, _Keypress, _Distance, _MUnits, _Animate }TimerList; 

//the timers
class Timers {
public:
	int GetTime(TimerList);
	void SetTime(TimerList, int);
	void DefineStartingTime(int);

protected:
	int PreLoopTime;
	int Loop;
	int Move;
	int PosAI;
	int IPS;
	int Keypress;
	int Distance;
	int MUnits;
	int Animate;
};


IG_Timers.cpp
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
#include "IG_Timers.h"

void Timers::DefineStartingTime (int StartTime)
{
	PreLoopTime = StartTime; //know when the loop begins
	Loop = PreLoopTime; //define used timers
	Move = PreLoopTime;
	PosAI = PreLoopTime;
	IPS = PreLoopTime;
	Keypress = PreLoopTime;
	Animate = PreLoopTime;
	MUnits = PreLoopTime;
	Distance = PreLoopTime;
}

int Timers::GetTime(TimerList TimerName)
{
	if (TimerName == _Loop)
		return Loop;
	else if (TimerName == _Animate)
		return Animate;
	else if (TimerName == _IPS)
		return IPS;
	else if (TimerName == _Keypress)
		return Keypress;
	else if (TimerName == _Move)
		return Move;
	else if (TimerName == _MUnits)
		return MUnits;
	else if (TimerName == _PosAI)
		return PosAI;
	else
		return -1;

}

void Timers::SetTime(TimerList TimerName, int NewTime)
{
	if (TimerName == _Loop)
		Loop = NewTime;
	else if (TimerName == _Animate)
		Animate = NewTime;
	else if (TimerName == _IPS)
		IPS = NewTime;
	else if (TimerName == _Keypress)
		Keypress = NewTime;
	else if (TimerName == _Move)
		Move = NewTime;
	else if (TimerName == _MUnits)
		MUnits = NewTime;
	else if (TimerName == _PosAI)
		PosAI = NewTime;
}


Main.cpp (just calling the functions)
1
2
3
4
5
6
7
8
9
10
#include "IG_Timers.h"
... ... ... ...

Timers::DefineStartingTime(100);

... ... ... ...

Timers::SetTime(_PosAI,Timers::GetTime(_PosAI));

... ... ... ...


The errors:

1
2
3
4
5
6
1>Main.cpp
error C2352: 'Timers::DefineStartingTime' : illegal call of non-static member function
see declaration of 'Timers::DefineStartingTime'

error C2352: 'Timers::GetTime' : illegal call of non-static member function
see declaration of 'Timers::GetTime'


Ive been playing around with static for the past few hours but what i understand is that static is the opposite from extern? Which means that i just get more errors if i use static. (so confuzzled)

Thanks in advance. I really like C++, but for some reason i will never be able to understand how to properly use multiple source files...
create an object of the class Timers in main()
1
2
3
Timers time;
time.DefineStartingTime(100);
time.SetTime(_PosAI, time.GetTime(_PosAI));
This issue has nothing to do with you using multiple files. When you call a function using :: the function must be declared static. Don't confuse this use of static with the at least 2 other uses of static in C++. Static here means that the function can be called without creating an object of the class.

I assume you don't want the the function to be static here. In that case, to call DefineStartingTime you must first create a Timers object. You can then use the dot syntax to call the function. Like this:
1
2
Timers t;
t.DefineStartingTime(100);
Last edited on
Thankyou :)
But... Doesnt that make a whole new clean Timers class in the Main object.
Meaning that i cant share the information between other files?

least 2 other uses of static in C++

No wonder static has been confusing me...
Last edited on
You should learn the difference between a class and an object. There is always only one Timers class. The class is the description of how the objects work. When you create a Timers object it will be independent of all other Timers object that you might have created.

If you want to share the same Timers between different parts of your program you can solve this in many different ways.
1. You can pass a pointer/reference to the Timers object to the functions that uses it.
2. You can create a global Timers object that can be reached from all parts of the program.
3. You can make all the members static and call the functions as you initially tried to do.
4. You can put everything in a namespace instead of a class.

I think (1) is the most generally recommended way and (2) is least recommended. (3) and (4) will work similar when used but the implementation of (4) is probably nicer.
Last edited on
Thankyou, ill read up on this.
Topic archived. No new replies allowed.