Why wont my class work?

Dec 18, 2014 at 4:38am
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:

};

Dec 18, 2014 at 8:49am
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 Dec 18, 2014 at 8:49am
Dec 18, 2014 at 12:15pm
@pnoid Is not declare your class, but create an object of type
Game
on the stack.
Dec 18, 2014 at 12:37pm
@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 Dec 18, 2014 at 12:39pm
Dec 18, 2014 at 6:41pm
Thanks guys!
Dec 18, 2014 at 11:40pm
@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.
Dec 19, 2014 at 12:08am
Line 5 of my lines of code does declare myGame on the process stack.
Dec 19, 2014 at 12:21am
honestly I feel kind of dumb because that was so simple can't even call my own function from a class ^^
Dec 19, 2014 at 1:04am
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.
Dec 19, 2014 at 3:30am
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 Dec 19, 2014 at 4:01am
Dec 20, 2014 at 12:21am
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.