Why wont my class work?

This is to completely avoid using any System(ANYTHING) in command promt.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Game.h"

using namespace std;

int main()
{
	Game run();
        //This will be removed once it works
	system("PAUSE");
	return 0;
}


Game.cpp
1
2
3
4
5
6
7
#include "Game.h"

void Game::run(){

	cout << "Hello";

};


Game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

class Game
{

public:

	Game();
	void run();

private:

};

You have to declare your class in main like any other variable.

Like this:
1
2
3
Game game;

game.run();


Replace line 8 in your code with something like that.
Last edited on
@pnoid Is not declare your class, but create an object of type
Game
on the stack.
@OP:
1
2
3
4
5
6
7
8
9
10
11
#include "Game.h"

int main()
{
    Game myGame;    // Declare an automatic Game variable 

    myGame.run();   // Ask myGame to run()

    return 0;

}



EDIT: Added missing include statement.
Last edited on
Thanks guys!
@pnoid Is not declare your class, but create an object of type
Game
on the stack.

I know, I was going to say something more along those lines, but I figured it would be easier to understand if it was compared to standard data types since the syntax is the same in this case.
Line 5 of my lines of code does declare myGame on the process stack.
honestly I feel kind of dumb because that was so simple can't even call my own function from a class ^^
Another thing to mention is you should first avoid using namespace ... especially in header files and secondly there is no need to #include <iostream> since you do not use anything from that header.
Well when I moved them I got an error.. and after looking over my code I was using them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef __GAME_H_INCLUDED__
#define __GAME_H_INCLUDED__

#include <iostream>
#include <string>

using namespace std;

class Game
{

public:

	Game();
	void Run();
	void Command(string &userInput, bool &gameRunning);
	void Help();

private:

};
#endif __GAME_H_INCLUDED__ 


Is there a reason why I don't want to put #includes in my headers files? or where you just pointing out that is was a waste because I wasn't using them?
Last edited on
I was pointing out it was a waste to include headers if you do not use them. Since in the first code you didn't have the string, but anyways all you really need is this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef __GAME_H_INCLUDED__
#define __GAME_H_INCLUDED__


#include <string>


class Game
{

public:

	Game();
	void Run();
	void Command(std::string &userInput, bool &gameRunning);
	void Help();

private:

};
#endif __GAME_H_INCLUDED__  


I was saying you should not put using namespace ... in a header because it can cause some pretty major naming conflicts.
Topic archived. No new replies allowed.