PDA

Click to See Complete Forum and Search --> : Classes!!


Zej
Aug 23rd, 2000, 04:39 PM
having trouble them them!! :mad:

this program has no purpose except to teach me about classes (incase you're wondering :D)

CPlayer Class

// Player.cpp: implementation of the CPlayer class.
//
//////////////////////////////////////////////////////////////////////
#include "iostream.h"
#include "Player.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPlayer::CPlayer()
{

}

CPlayer::~CPlayer()
{

}

void CPlayer::jump()
{
cout << "Player jumped!";
}


Now here is the main part of my code.

#include <iostream>
using namespace std;

void main()
{
CPlayer guy;
guy.jump(); // The guy suppose to JUMP here but he doesn't!!!
}


for some reason this ain't working so could someone spot a mistake in here :confused:

CthulhuDragon
Aug 23rd, 2000, 05:07 PM
I don't spot anything unusual immediately. However without your class declaration I can't be sure. I noticed in your main that you have
#include <iostream>
using namespace std;

but in your CPlayer class you have:
#include "iostream.h"

Why?

iostream (without the .h) isn't needed unless you plan on using stl in your project. Either way you should use one or the other.

Using this main
#include <iostream>
#include "Player.h"
using namespace std;

void main()
{
CPlayer guy;
guy.jump(); // The guy suppose to JUMP here but he doesn't!!!
}

This Header:
class CPlayer{
public:
CPlayer();
~CPlayer();
void jump();
};

and This CPP:
#include "iostream.h"
#include "Player.h"

CPlayer::CPlayer()
{

}

CPlayer::~CPlayer()
{

}

void CPlayer::jump()
{
cout << "Player jumped!";
}

It works fine. I get "Player jumped!" on the screen

[Edited by CthulhuDragon on 08-24-2000 at 10:46 AM]

Zej
Aug 23rd, 2000, 06:57 PM
Thanks CthulhuDragon!! i fogot to include the "player.h" in my main file, so now it works like a charm! :p.
the reason I used <iostream> is because someone said you can use this too. it works fine both ways now so thanks again!!

parksie
Aug 24th, 2000, 12:47 PM
I beg to differ on the iostream issue. It isn't part of the STL, but of the Standard C++ Library, which includes the IOStream library, string streams, and STL. I always use the std:: headers, since they are actually better than Microsoft's own one. (Actually, MS hint that you should use the std:: ones).