help with code

hi
i keep getting an error about health speed and strength i know what the problem is but i am not sure how to fix it can someone help me

#pragma once
#include<iostream>
#include<stdio.h>
using namespace std;

class characters
{
float health;
int strength;
int speed;
public:
virtual void print()
{
}
};
class fighter1:public characters
{
fighter1(float h,int st, int sp):health(h),strength(st),speed(sp)
{
h = 10;
st = 5;
sp = 5;
}
virtual void print()
{

}
};
If this is going to be a header, do not - please, please do not - stick using namespace std; at the top.


Anyway, onwards. Only non-inherited variables can be set in an initialisation list. If you want those variables to be set, you have to get the base class' constructor to do it. You don't provide one, but if you did, it would look something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class characters
{
  float health;
  int strength;
  int speed;
  public:
  virtual	void print()
  {
  }

  characters(float h, float st, float sp)
  { 
    health = h;
    strength = st;
    speed = sp;
  }

};

class fighter1:public characters
{
  fighter1(float h,int st, int sp):characters(h, st, sp)
  {
  }

  virtual void print()
  {
  }
};

Last edited on
thank you most appreciated i keep making that same mistake
Topic archived. No new replies allowed.