-
Class scope issues
I can't seem to figure out why my bullet.h can't see the Player class and my bullet.cpp can. It just says a namespace with this name does not exist.
Bullet.h
Code:
#ifndef BULLET_H
#define BULLET_H
#include "stdafx.h"
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include "SOIL.h"
#include "Player.h"
using namespace PLAYER;
class Bullet
{
public:
Bullet(Player player);
void Draw();
void Update();
};
#endif
Bullet.cpp
Code:
#include "stdafx.h"
#include "Bullet.h"
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include "SOIL.h"
#include "Player.h"
using namespace PLAYER;
Bullet::Bullet(Player player)
{
}
Player.h
Code:
#ifndef PLAYER_H
#define PLAYER_H
#include "stdafx.h"
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include "SOIL.h"
#include "Bullet.h"
namespace PLAYER
{
class Player
{
public:
struct Vector3
{
float X, Y, Z;
Vector3() {}
Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
};
struct Color
{
float R,G,B;
Color(){}
Color(float r, float g, float b)
{
R = r;
G = g;
B = b;
}
};
Player();
Player(Vector3 StartPos, Color StartColor);
void Update(bool keys[256], bool prevkeys[256], float deltaTime);
void Draw();
Vector3 Position;
float yrot, xrot;
Vector3 getDirectionVector();
bool TouchingGround();
private:
void HandleKeyboard(bool keys[256], bool prevkeys[256]);
Color color;
POINT curMouse, prevMouse;
float velocity;
bool wantToJump;
bool FreeMode;
};
};
#endif
Player.cpp
Code:
#include "stdafx.h"
#include "Player.h"
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include "SOIL.h"
#include <math.h>
using namespace PLAYER;
const float piover180 = 3.1469f/180;
const float gravity = -9.81f;
const float height = 3;
const float terminalVelocity = 10;
Player::Player()
{
}
Player::Player(Player::Vector3 StartPos, Color StartColor)
{
Position = StartPos;
color = StartColor;
GetCursorPos(&prevMouse);
xrot = 90;
yrot = 0;
velocity = 0;
Position.Y += height;
wantToJump = false;
FreeMode = false;
}
void Player::Update(bool keys[256], bool prevkeys[256], float deltaTime)
{
Player::HandleKeyboard(keys, prevkeys);
if(!FreeMode)
{
if(!TouchingGround())
velocity += gravity*.02f;
if(velocity > terminalVelocity)
velocity = terminalVelocity;
if(velocity < 0 && TouchingGround())
{
velocity = 0;
}
if (Position.Y < height-1)
Position.Y = height-1;
//if((Position.Y - velocity) < height)
//velocity = 0;
if(wantToJump)
{
velocity = 4;
wantToJump = false;
}
Position.Y += velocity*.03f;
}
}
void Player::HandleKeyboard(bool keys[256], bool prevkeys[256])
{
GetCursorPos(&curMouse);
SetCursorPos(1280/2,768/2);
prevMouse.x = 1280/2;
prevMouse.y = 768/2;
xrot -= (prevMouse.y - curMouse.y) * .15f;
yrot -= (prevMouse.x - curMouse.x) * .15f;
if(xrot > 179)
xrot = 179;
if(xrot < 1)
xrot = 1;
Player::Vector3 DirMov;
if(!FreeMode)
DirMov = Player::Vector3(sin(yrot * piover180),0,-cos(yrot*piover180));
else if(FreeMode)
DirMov = Player::Vector3(sin(yrot*piover180)*sin(xrot*piover180),cos(xrot*piover180),-cos(yrot*piover180)*sin(xrot*piover180));
Player::Vector3 RightMov = Player::Vector3(sin((yrot * piover180) + 3.1469f/2),0,-cos((yrot*piover180) + 3.1469f/2));
if(keys['W'])
{
Position.Z += DirMov.Z*.09f;
Position.X += DirMov.X*.09f;
Position.Y += DirMov.Y*.09f;
}
if(keys['S'])
{
Position.Z -= DirMov.Z*.09f;
Position.X -= DirMov.X*.09f;
Position.Y -= DirMov.Y*.09f;
}
if(keys['A'])
{
Position.Z -= RightMov.Z*.09f;
Position.X -= RightMov.X*.09f;
}
if(keys['D'])
{
Position.Z += RightMov.Z*.09f;
Position.X += RightMov.X*.09f;
}
if(keys[VK_SPACE] && !wantToJump && TouchingGround() && !prevkeys[VK_SPACE] && !FreeMode)
{
wantToJump = true;
}
if(keys[VK_NEXT])
Position.Y -= 0.1f;
if(keys[VK_PRIOR])
Position.Y += 0.1f;
if(keys[VK_TAB] && !prevkeys[VK_TAB] && !FreeMode)
FreeMode = true;
else if(keys[VK_TAB]&& !prevkeys[VK_TAB] && FreeMode)
FreeMode = false;
prevMouse = curMouse;
}
Player::Vector3 Player::getDirectionVector()
{
Player::Vector3 tmp;
tmp.Y = Position.Y + cos(xrot *piover180);
tmp.X = Position.X + (sin(yrot * piover180) * sin(xrot * piover180));
tmp.Z = Position.Z - (cos(yrot * piover180) * sin(xrot * piover180));
return tmp;
}
bool Player::TouchingGround()
{
if(Position.Y <= height-1 || (Position.Y <= height-1 + 2 && Position.X >= -1 && Position.X <= 1 && Position.Z <= 1 && Position.Z >= -1))
{
return true;
}
return false;
}
-
You have a recursive dependency: bullet.h includes player.h which includes bullet.h... which includes player.h which includes bullet.h, ad infinitum.
In this case, it looks like neither header files needs the definition of the class defined in the other, so a forward declaration will do, e.g. in bullet.h:
Code:
namespace PLAYER
{
class Player;
}
By the way, do not place using directives like using namespace PLAYER; at file scope in a header file. The authors of source files that include your header should be free to decide whether or not they want to qualify names from the namespace, or use their own using directive or using declaration.
Also, it would be wise to use some other convention for naming your namespaces as fully capitalised names are generally used for macros, or at least for constants.
-
Thank you for letting me know I was using namespace and includes in the wrong way. I will also work on my conventions.
-
Arrays are a touchy subject in C++. I suggest you familiarize yourself with the methods and information described here: SourceForge.net: Safer arrays in Cpp - cpwiki