delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/03/21/12:01:56

From: "Daniel Eliasson" <danieleliasson AT chello DOT se>
Newsgroups: comp.os.msdos.djgpp
Subject: Need some class help
Lines: 174
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
Message-ID: <NY4u6.34$I81.1046@nntp1.chello.se>
Date: Wed, 21 Mar 2001 16:49:49 GMT
NNTP-Posting-Host: 193.150.238.248
X-Complaints-To: abuse AT chello DOT se
X-Trace: nntp1.chello.se 985193389 193.150.238.248 (Wed, 21 Mar 2001 17:49:49 MET)
NNTP-Posting-Date: Wed, 21 Mar 2001 17:49:49 MET
Organization: chello broadband
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi... I'm rather new to C++, but I'm trying to code a small game. The
problem however is that I get some rather peculiar (in my view) errors when
I try to compile player.cpp

player.cpp:20: syntax error before `{'
player.cpp:32: ANSI C++ forbids declaration `Player' with no type
player.cpp: In function `int Player(char * = "Mr. I\'m too lazy to name
myself.
I suck.")':
player.cpp:38: type mismatch with previous external decl
player.cpp:28: previous external decl of `char * weaponCache[10]'
player.cpp:38: storage size of `weaponCache' isn't constant
player.cpp:51: invalid types `int[int]' for array subscript
player.cpp: At top level:
player.cpp:61: parse error before `}'
player.cpp:65: syntax error before `::'
player.cpp:70: syntax error before `::'
player.cpp:74: ANSI C++ forbids declaration `pGetY' with no type
player.cpp:87: syntax error before `::'
player.cpp:95: parse error before `;'
player.cpp:95: syntax error before `++'
player.cpp:97: parse error before `;'
player.cpp:97: syntax error before `++'
player.cpp:118: syntax error before `::'
player.cpp:127: syntax error before `++'
player.cpp:135: syntax error before `--'
player.cpp:143: syntax error before `++'
player.cpp:151: syntax error before `--'


And the code:

#include "game.h"

Class Player
{
    public:

    const int MAXHEALTH = 1000;
    const int MAXWEAPONS = 10;
    int health;
    char *currentWeapon;
    char *weaponCache[] = new char*[MAXWEAPONS];
    char *name;

    Player(char *pName = "Mr. I'm too lazy to name myself. I suck.")
    {
 maxHealth = MAXHEALTH;
 health = MAXHEALTH;
// currentWeapon = "";
 name = pName;

 for(int i = 0; i < MAXWEAPONS; i++)
 {
     weaponCache[i] = "";
 }

 map[1][GRIDSIZE - 1] = GRID_PLAYER;
    }

    // These are the definitions of the member functions of Player... the
names should be self-explanatory
    int changeWeapon(char *pNewWeapon);
    int fireWeapon(void);
    void movePlayer(char *direction);
    void getPosition(int *pX, int *pY);
    void setPosition(int *pX, int *pY);
    char* getName();
};



char* Player::getName()
{
    return this.name;
}

void Player::getPosition(int *pGetX, int *pGetY)
{
    // Null the variables to start from coordinates 0,0
    pGetX = 0;
    pGetY = 0;

    // Get players position and store the coordinates in pGetX and pGetY
/*    while(map[pGetX][pGetY] != GRID_PLAYER)
    {
 while(map[pGETX][pGetY] != GRID_PLAYER && pGetY < GRIDSIZE)
 {
  pGetY++;
 }
        pGetX++;
    }
*/

void Player::setPosition(int *pSetX, int *pSetY)
{
    // Check if the location the player wants to move to is indeed valid and
empty.
    if(pSetX >= 0 && < GRIDSIZE) && if(pSetY >= 0 && < GRIDSIZE) &&
if(map[pSetX][pSetY] == GRID_EMPTY || GRID_ AMMO || GRID_WEAPON)
    {

 // If it is, clear old position by searching throught the grid and removing
the player
        // Check if the location the player wants to move to is indeed valid
and empty
 for(int i = 0; i < GRIDSIZE; i++)
 {
     for(int j = 0; j < GRIDSIZE; j++)
     {
  if(map[i][j] == GRID_PLAYER)
  {
      map[i][j] = GRID_EMPTY;
  }
     }
 }


 // Then add the player to the new position

 if (map[pSetX][pSetY]
 map[pSetX][pSetY] = GRID_PLAYER;
 return 1;
    }
    else
 return 0;
}

// Checks for keyboard input and uses it to move player
void Player::movePlayer(char *direction)
{
    int x, y;

    switch(direction)
    {
 case "RIGHT" :
 {
     this.getPosition(&x, &y);
     x++;
     this.setPosition(&x, &y);
     break;
 }

 case "LEFT" :
 {
     this.getPosition(&x, &y);
     x--;
     this.setPosition(&x, &y);
     break;
 }

 case "UP" :
 {
     this.getPosition(&x, &y);
     y++;
     this.setPosition(&x, &y);
     break;
 }

 case "DOWN" :
 {
     this.getPosition(&x, &y);
     y--;
     this.setPosition(&x, &y);
     break;
 }

 default :
     break;
    }
}



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019