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

Started by
6 comments, last by jpetrie 8 years ago

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???

Advertisement
Perhaps a mutual #include? Try this:


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

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.

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();
	}

}
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.

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..?

The code you show in post #4 includes Time.h, not Timer.h.

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.

This topic is closed to new replies.

Advertisement