Sign in to follow this  
BiiXteR

"Timer undeclared identifier" Is my compiler drunk? o.o

Recommended Posts

I have this piece of code : 

#pragma once
#include <SDL\SDL.h>
#include <iostream>
#include <vector>
#include "Timer.h"

class Time
{
public:
	Time();
	~Time();

	static void Update();
	static double GetDeltaTime();

	static void AddTimer(Timer* timer);

private:

	static std::vector<Timer*> timer_list;
	static double deltaTime;
	static double lastFrameElapsedTime;
	static double elapsedTime;

};

As you can see on top, it says include "Timer.h", in Timer.h there's a Timer class.

 

On line 16, Visual Studio yells at me because : 'Timer': undeclared identifier

 

But it's included right there???

Share this post


Link to post
Share on other sites

To expand a bit... remember that in C++, the compiler compiles each source file in isolation, and that generally header files are not source files (rather, they are inserted into a source file during preprocessing).

 

Your Timer.cpp probably has "#include "Timer.h" at the top. Nypyren theorizes (and I agree) that Timer.h has an #include for Time.h someplace, and Time.h has an include for Timer.h as you've demonstrated.

 

Thus, when Timer.cpp is preprocessed, it will first insert the contents of Timer.h, and recursively continue preprocessing. This means it will see the newly-inserted include for Time.h, insert than, and recursively continue preprocessing. It will see the include of Timer.h (inserted via Time.h) and attempt to insert it, but you've used include guards so it will just skip the insertion. The modified Time.cpp now contains, in order:

 

The contents of Time.h.

The contents of Timer.h.

The contents of Time.cpp.

 

Since the compiler was prevented from recursively including Timer.h again before Time.h, the code making use of Timer* in Time.h cannot be compiled, because in that source file Timer hasn't been declared yet.

 

You can work around this, as suggested, by removing the include for Timer.h in Time.h and instead using a forward declaration.

Share this post


Link to post
Share on other sites

Perhaps a mutual #include? Try this:
 

//#include "Timer.h"
class Timer;
Move the #include to your CPP file (or your PCH, if applicable).

 

 

That fixed some of the errors, but now I'm getting : 'timer' uses undefined class 'Timer' when using the Timer class inside of my Game.cpp?

I'm also getting incomplete type is not allowed.

 

Both of these at line ; 23

#include "Game.h"
#include "Input.h"
#include "Sprite.h"
#include "Vector2.h"
#include "Time.h"

bool Game::isRunning;

Game::Game()
{
	window.CreateWindow();
	isRunning = true;

	world = new World();
	world->CreateWorld();

	Input::InitializeInput();

	SpriteManager::CreateSprite("sprite1", "src/images/Dev_Block.bmp", Vector2(25, 25), Vector2(50, 50), 1.0f, 0.3f, b2_dynamicBody);
	SpriteManager::CreateSprite("sprite2", "src/images/Dev_Block.bmp", Vector2(25, 25), Vector2(50, 50), 1.0f, 0.3f, b2_dynamicBody);
	SpriteManager::CreateSprite("sprite3_static", "src/images/Dev_Block.bmp", Vector2(0, 300), Vector2(500, 20), 1.0f, 0.3f, b2_staticBody);

	Timer timer;;
	timer.Start(10);

	while (isRunning == true)
	{
		std::cout << timer.GetCurrentTime() << std::endl;
		Update();
		Draw();
	}

}
Edited by BiiXteR

Share this post


Link to post
Share on other sites

You will need to include Timer.h wherever you are using Timer. A forward declaration is not enough for creating an instance (since the compiler knows neither how much much memory to allocate nor what constructor to call) or calling a member function.

 

Not sure what you mean?

Where should I include it? I'm already including it on top of my cpp file..?

Share this post


Link to post
Share on other sites

 

You will need to include Timer.h wherever you are using Timer. A forward declaration is not enough for creating an instance (since the compiler knows neither how much much memory to allocate nor what constructor to call) or calling a member function.

 

Not sure what you mean?

Where should I include it? I'm already including it on top of my cpp file..?

 

 

 

You're not including it at the top of the .cpp file you posted here. You include Time.h, but not Timer.h. You will need Timer.h if you want to use Timer the way you do on line 23. Otherwise you're only bringing a forward declaration of Timer into scope (via Time.h); you're asserting that Timer exists and is a class, but you're not saying anything else about it (such as what methods it has). That requires the full definition of Timer, which is in Timer.h. C++ requires the full definition of a type before you can create instances of that type or call methods on it, like you're doing.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this