Thread: Class scope issues

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    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;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 05-27-2013 at 08:37 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    2
    Thank you for letting me know I was using namespace and includes in the wrong way. I will also work on my conventions.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Scope
    By strokebow in forum C++ Programming
    Replies: 7
    Last Post: 12-17-2008, 05:45 PM
  2. c++ class scope issues
    By avatarofhope2 in forum C++ Programming
    Replies: 13
    Last Post: 10-23-2007, 07:20 PM
  3. Class not declared in the scope
    By DarrenY in forum C++ Programming
    Replies: 6
    Last Post: 08-27-2007, 11:39 AM
  4. Class scope variables
    By filler_bunny in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2003, 02:14 AM
  5. class scope
    By Kohatian 3279 in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2002, 01:28 PM

Tags for this Thread