FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Object-oriented troubles

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Sat Apr 18, 2009 8:49 pm    Post subject: Object-oriented troubles Reply with quote

I had a similar problem before, but it was easily solved. This time, I don't know what's wrong. I have a few classes for a basic RPG type battle.

Character:
Code:

//This will be a character super class for possible characters in an RPG game.

//This is a simple example for the project. At a later date, I hope to include a stats system so that
//character-specific methods may be better overridden. For now, I just used "abstract" methods.

module Character;

import std.stdio;
import AJRandom; //random numbers

public class Character {
   
   //Instance variables
   protected:
      final string NAME; //The character's name
      int health; //The character's HP
      int maxHealth; //Maximum health
      AJRandom gen; //Random generation
   
   //Methods
   public:
      //Constructor. Blank for this superclass
      this(){
         gen = new AJRandom();
      }
      
      //Method to reduce the health of this character
      void takeDamage(int n){
         this.health -= n;
      }
      
      //Method to inflict damage on an enemy
      void dealDamage(Character other, int n){
         other.takeDamage(n);
         writefln("%d attacks %d for %d damage!", this.NAME, other.getName(), n);
      }
      
      void attack();
      
      //Method to self-heal
      void heal(int n){
         if((this.health - n) < this.maxHealth){
            this.health += n;
         } else {
            this.health = this.maxHealth;
         }
         writefln("%d rests and heals %d HP", this.NAME, n);
      }
      
      void selfHeal();
      
      void printStats(){
         writefln("%d has %d HP remaining.", this.NAME, this.health);
      }
      
      string getName(){
         return this.NAME;
      }
   
}


Mitsuki:
Code:

//Mitsuki is a character, thus, she will extend from the Character class.

module Mitsuki;

import Character;

//We use a colon (:) to indicate that Mitsuki inherits from Character
public class Mitsuki : Character {
   
   //Character-specific constructor.
   public:
      this(){
         this.NAME = "Mitsuki";
         this.health = this.maxHealth = 100;
      }
      
      //Override the abstract attack and selfHeal methods
      void attack(Character other){
         //Mitsuki's attacks will do from 10 to 20 damage
         this.dealDamage(other ,10 + this.gen.getNumber() % 11);
      }
      
      void selfHeal(){
         //Mitsuki can heal herself from 5 to 30 HP
         this.heal(5 + this.gen.getNumber() % 26);
      }
}


Enemy:
Code:

//An enemy for Mitsuki to fight

module Enemy;

import Character;

public class Enemy : Character {
//   Character-specific constructor.
   public:
      this(){
         this.NAME = "Enemy";
         this.health = this.maxHealth = 75;
      }
      
      //The client can give the enemies names with this overrided constructor
      this(string onamae){
         this.NAME = onamae;
         this();
      }
      
      void attack(Character other){
         //Enemy's attacks will do from 5 to 40 damage
         this.dealDamage(other ,5 + this.gen.getNumber() % 36);
      }
      
      void selfHeal(){
         //The enemy can heal from 2 to 10 HP
         this.heal(2 + this.gen.getNumber() % 9);
      }
}




AJRandom
Code:

//Class to return a random number

module AJRandom;

import std.stdio;
import std.c.stdlib;
import std.c.time;

public class AJRandom {
   public:
      this();
      
      //Method to generate a random number
      int getNumber(){
         srand(time(null));

         int number = rand();
         
         return number;
      }
}


And finally, the GameClient

Code:

module GameClient;

import std.stdio; //I must remember the import declarations go under module
import Character;
import Enemy;
import Mitsuki;

void main(string[] args){
   Mitsuki mitsuki = new Mitsuki();
   
   mitsuki.printStats();
}


I get the following printout:
Code:

OPTLINK (R) for Win32  Release 8.00.1
Copyright (C) Digital Mars 1989-2004  All rights reserved.
.\_Character.obj(_Character)
 Error 42: Symbol Undefined _D8AJRandom8AJRandom5_ctorMFZC8AJRandom8AJRandom
.\_Character.obj(_Character)
 Error 42: Symbol Undefined _D9Character9Character8selfHealMFZv
.\_Character.obj(_Character)
 Error 42: Symbol Undefined _D9Character9Character6attackMFZv
--- errorlevel 3
Back to top
View user's profile Send private message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Sat Apr 18, 2009 9:00 pm    Post subject: Reply with quote

Well, I fixed it by changing the empty methods from method(); to
Code:

method() {

}


Now, when I try to run it, I get the following erro (it compiles but actually running the .exe):
Error: std.format string

Can anyone tell me what's wrong?
Back to top
View user's profile Send private message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Sat Apr 18, 2009 9:04 pm    Post subject: Reply with quote

Nevermind. I have to use "%s" for strings and %d" for numbers. I'm very sorry for wasting your time. Thanks anyway.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group