C++::Call a function from another source file?

Recommended Posts

nibbuler    162
From Main.cpp (Click to enlarge) Here's some of Player.h I declared class Player in Player.h, defined it in Player.cpp, but I can't call it in Main.cpp... [Everything is in stdafx.h, and both source files #include it] I need to do this in the main loop... Not sure how to call it altogether because even when I put everything from Player.cpp into Main.cpp, and use Player::handle_input(); it says that it's illegal. What's the syntax to do such a thing? (Preferably to call it from Player.cpp in the Main.cpp loop) Help is much appreciated.

Share on other sites
Crypter    748
The smaller image displays that you are treating Player as an object, but you said you have declared it as a class.

Do you declare an object for type Player? If not, then this is the problem. A class describes an object, it does not create it. Because of this, Player.some_method() is incorrect.

Share on other sites
nibbuler    162
Awesome, mate. That's all it took... adding
Player Player;
to main().

Thanks a ton!

Share on other sites
Captain P    1092
Quote:
 Original post by nibbulerAwesome, mate. That's all it took... adding Player Player; to main().Thanks a ton!

I would not recommend naming an instance the same as it's class. That's going to add confusion, if it's even allowed to do so. At least go for lowercase for the instance names, or give them a different (but descriptive) name altogether.
Player player;
or
Player bob;
would be better.

Create an account

Register a new account