I'm a noobie at C++ and I was making a game for practice on Visual Studios and I just couldn't figure out how to update stats when exp was added. I tried changing the player level by adding exp but when I add 55 exp the player remained at level 1 still.
Main:
#include <iostream>
#include <windows.h>
#include "Game.h"
using namespace std;
void FalseLoad();
int main() {
//Cool load intro
FalseLoad();
cout << "\n \n";
Game::Game();
system("PAUSE");
return 0;
}
void FalseLoad() {
int i = 0;
int start;
cout << "***Freelancer*** \n \n";
system("PAUSE");
while (i <= 100){
cout << "Loading game... " << i << "% \n";
i++;
Sleep(110 - i);
if (i == 100) {
start = 0;
}
}
}
Game.cpp:
#include <iostream>
#include "Game.h"
using namespace std;
Game::Game() {
Player Player;
Player.Init();
cout << Player.exp << " " << Player.level;
Player.exp += 55;
cout << " " << Player.exp << " " << Player.level << " ";
}
Game.h:
#pragma once
#include "Player.h"
class Game {
public:
Game();
};
Player.cpp:
#include "Player.h"
Player::Player() {
}
void Player::Init() {
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
}
Player.h:
#pragma once
class Player
{
public:
Player();
void Init();
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
};
If you add 55 to exp, only exp will be changed.
You can write getters and setters and declare the member variables private:
Player.h
#pragma once
class Player
{
public:
Player();
void Init();
void addExp(const int additionalExp);
int getExp();
//... TODO add similar get/set methods for the other members...
private:
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
};
and add the method definitions:
#include "Player.h"
Player::Player() {
}
void Player::Init() {
int exp = 5;
int level = (exp / 5);
int attack = (10 + (level * 2));
int defense = (10 + (level * 2));
int speed = (10 + (level * 2));
}
void Player::addExp(const int additionalExp) {
if ( additionalExp < 0 ) return; // think about error handling or use
// unsigned for exp
exp += additionalExp;
level = exp / 50; // or something else, as you like.
}
int Player::getExp(){ return exp; }
// ... TODO add definitions for the other get/set methods...
And use the addExp() method in your main.cpp.
One benefit of having the member variables private is that you get more control in how they get manipulated. E.g. if you add exp, you can set level accordingly simultaneously.
Related
Vehicle class is an abstract base class for ElectricVehicle and GasolineVehicle classes, and HybridVehicle class inherits from ElectricVehicle and GasolineVehicle.
I have two 'Vehicle':base class undefined errors in ElectricVehicle.h and GasolineVehicle.h. I know I am messing up the #include files somehow, but I'm having trouble figuring that out.
I tried removing the Vehicle.cpp in ElectricVehicle.cpp and GasolineVehicle.cpp and adding the Vehicle header file, but that came up with a bunch of other errors.
Vehicle.h
#pragma once
#ifndef Vehicle_h
#define Vehicle_h
class Vehicle {
public:
// check here // 4 lines
float engineEfficiency;
virtual float calculateRange() = 0;
virtual float percentEnergyRemaining() = 0;
virtual void drive(float) = 0;
virtual ~Vehicle();
};
#endif
Vehicle.cpp
#pragma once
#include "Vehicle.h"
#include <iostream>
using namespace std;
Vehicle::~Vehicle() {
cout << "In vehicle destructor" << endl;
}
ElectricVehicle.h
#pragma once
#include <iostream>
#ifndef ElectricVehicle_h
#define ElectricVehicle_h
class ElectricVehicle : virtual public Vehicle {
public:
float maximumCharge;
float currentCharge;
float engineEfficiency;
public:
ElectricVehicle(float max, float eff);
float calculateRange();
float percentEnergyRemaining();
void drive(float km);
~ElectricVehicle();
};
#endif
ElecticVehicle.cpp
#pragma once
#include "Vehicle.cpp"
#include "ElectricVehicle.h"
using namespace std;
ElectricVehicle::ElectricVehicle(float maxEnergy, float rating) {
engineEfficiency = rating;
maximumCharge = maxEnergy;
currentCharge = maxEnergy;
}
ElectricVehicle::~ElectricVehicle() {
cout << "In Electric vehicle destructor" << endl;
}
float ElectricVehicle::calculateRange() {
return (currentCharge * 100 / engineEfficiency);
}
float ElectricVehicle::percentEnergyRemaining() {
return (currentCharge * 100.0f / maximumCharge);
}
void ElectricVehicle::drive(float km) {
if (currentCharge < 0) {
cout << "Your Car is out of energy";
return;
}
currentCharge -= (km / 100) * engineEfficiency;
}
GasolineVehicle.cpp
#include "Vehicle.cpp"
#include "GasolineVehicle.h"
using namespace std;
GasolineVehicle::GasolineVehicle(float maxEnergy, float rating) {
engineEfficiency = rating;
maximumGasoline = maxEnergy;
currentGasoline = maxEnergy;
}
GasolineVehicle::~GasolineVehicle() {
cout << "In Gasoline vehicle destructor" << endl;
}
float GasolineVehicle::calculateRange() {
return currentGasoline * 100 / engineEfficiency;
}
float GasolineVehicle::percentEnergyRemaining() {
return (currentGasoline * 100.0f / maximumGasoline);
}
void GasolineVehicle::drive(float km) {
if (currentGasoline < 0){
cout << "Your Car is out of energy";
return;
}
currentGasoline -= (km / 100) * engineEfficiency;
}
Gasoline.h
#pragma once
#include <iostream>
#ifndef GasolineVehicle_h
#define GasolineVehicle_h
class GasolineVehicle : virtual public Vehicle {
public:
float maximumGasoline;
float currentGasoline;
float engineEfficiency;
GasolineVehicle(float max, float eff);
~GasolineVehicle();
float calculateRange();
float percentEnergyRemaining();
void drive(float km);
};
#endif
HybridVehicle.cpp
#pragma once
#include "ElectricVehicle.h"
#include "GasolineVehicle.h"
#include "HybridVehicle.h"
#include<iostream>
using namespace std;
// hybridvehicle constructor is calling its parent class constructors;
HybridVehicle::HybridVehicle(float maxGasoline, float gasefficiency, float maxcharge, float electricefficiency) :GasolineVehicle(maxGasoline, gasefficiency), ElectricVehicle(maxcharge, electricefficiency) {}
HybridVehicle::~HybridVehicle() {
cout << "In Hybrid vehicle destructor" << endl;
}
// :: is scope resolution operator as both gasoline and electric vehicle classes having engine efficiency the compiler
// will be confused of which variable to take.. so we :: to remove ambuiguity
float HybridVehicle::calculateRange() {
return (currentCharge / ElectricVehicle::engineEfficiency) * 100 + (currentGasoline / GasolineVehicle::engineEfficiency) * 100;
}
float HybridVehicle::percentEnergyRemaining() {
return ((currentCharge + currentGasoline) / (maximumCharge + maximumGasoline)) * 100.0f;
}
void HybridVehicle::drive(float km) {
if (currentGasoline + currentCharge < 0) {
cout << "Your Car is out of energy";
return;
}
else if (currentCharge > 0) {
currentCharge -= (km / 100) * ElectricVehicle::engineEfficiency;
}
else
{
currentGasoline -= (km / 100) * GasolineVehicle::engineEfficiency;
}
}
HybridVehicle.h
#pragma once
#ifndef HybridVehicle_h
#define HybridVehicle_h
class HybridVehicle: public GasolineVehicle, public ElectricVehicle {
public:
HybridVehicle(float gMax, float gEff, float eMax, float eEff);
~HybridVehicle();
float calculateRange();
float percentEnergyRemaining();
void drive(float km);
};
#endif
Week2.cpp
#pragma once
#include "Vehicle.cpp"
#include "HybridVehicle.cpp"
using namespace std;
Vehicle* testVehicle(Vehicle* pVehicle, const char* vehicleName) {
cout << vehicleName << " s range is: " << pVehicle->calculateRange() << endl;
pVehicle->drive(150);
cout << vehicleName << " s energy left is: " << pVehicle->percentEnergyRemaining() << endl;
cout << vehicleName << " s range is now: " << pVehicle->calculateRange() << endl;
return pVehicle;
}
int main(int argc, char** argv)
{
//50L of gas, 7.1 L/100km
delete testVehicle(new GasolineVehicle(50, 7.1), "Corolla");
//42 L of gas, 4.3 L/100km, 8.8kWh, 22 kWh/100km
delete testVehicle((GasolineVehicle*)new HybridVehicle(42, 4.3, 8.8, 22.0), "Prius");
//75 kWh, 16 kWh/100km
delete testVehicle(new ElectricVehicle(75, 16), "Tesla 3");
return 0;
}
This question already has answers here:
string and int concatenation in C++ [duplicate]
(3 answers)
Closed 2 years ago.
Hello I am trying to make a game somewhat like Pokemon but I'm stuck on printing stats.
I am trying to output a level with it showing what level the animal reached.
Any help on the fact I'm not getting expected results?
My code:
visualanimal.h (not used yet):
#include <string>
#ifndef VISUALANIMAL_H
#define VISUALANIMAL_H
using namespace std;
using namespace Animals;
/*** Visual based on original class
#author Adam Petla
***/
class VisualAnimal : Animal {
public:
string imageFileURL;
int size;
VisualAnimal() {
this->imageFileURL = "";
this->size = 0;
}
};
#endif
animal.h :
#include <string>
#include <stdio.h>
#include <iostream>
#ifndef ANIMAL_H
#define ANIMAL_H
using namespace std;
namespace Animals {
class Animal {
public:
string name;
int minlevel;
int level;
int maxlevel;
int baseattack;
int baseattackraise;
int attack;
int basedefense;
int basedefenseraise;
int defense;
int basespeed;
int basesppedraise;
int speed;
int basespecial;
int basespecialraise;
int special;
char type;
Animal() {
name = "DOG";
minlevel = 0;
level = 1;
maxlevel = 100;
baseattack = 1;
baseattackraise = 1;
basedefense = 1;
basedefenseraise = 1;
basespecial = 1;
basespecialraise = 1;
basespeed = 1;
basesppedraise = 1;
};
private:
void printstats() {
//cout << "Attack : " << this->
};
void raiseattack() {
this->attack += this->baseattackraise;
}
void raisedefense() {
this->defense += this->basedefenseraise ;
}
void raisespeed() {
this->speed += this->basesppedraise;
}
void raisespecial() {
this->special += this->basespecialraise;
}
void raisestats() {
raiseattack();
raisedefense();
raisespeed();
raisespecial();
}
void updatestats(char type) {
switch (type) {
case 'l':
raisestats();
}
}
public :
void raiselevel() {
this->level++ ;
this->type = 'l';
updatestats(this->type);
string output = "Level Up!Your " + string(this->name) + string("has reached level");
cout << output;
cout << this->level + ".Its stats are now";
printstats();
};
};
}
#endif
animal.cpp :
// Animals.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include "animal.h"
#include "visualanimal.h"
using namespace std;
int main()
{
using namespace`` Animals;
Animal test;
test.raiselevel();
cout << "Hello World!\n";
return -1;
}
My expected results: DOG reached level 2.
My actual results DOG reached level
Anybody know an answer
It doesn't show any level in output anybody know why?
Change the following:
cout << this->level + ".Its stats are now";
To:
cout << this->level << ".Its stats are now";
You were getting incorrect output because you are trying to concatenate a string and an int.
The correct way to concatenate a string to an int is using std::to_string():
int val = 2;
std::string x = "hello";
std::string output = std::to_string(val) + x;
+ has a higher precedence than <<, so the line cout << this->level + ".Its stats are now"; is evaluated as cout << (this->level + ".Its stats are now"); - the value of level is used as an offset into a string literal ".Its stats are now", which is not what you want.
Change it to
cout << this->level << ".Its stats are now";
Or see here how to concatenate an int with a string.
I'm new at programming. I wondered if you were to call a specific argument in the parameter for your constructor, which you had to use in your method, then how would you do it with pointers? I solved it by the use of 'this' because the local variable name is the same as the member variable's name, but I just wanted to see how it is possible by the use of normal pointers and not the this pointer.
Here is the code I worked on.
BMR.h
#include <string>
using namespace std;
class BMR
{
public:
BMR();
BMR(char, int, int, int);
int calculateBMR();
private:
int weight;
int height;
int age;
char gender;
};
BMR.cpp
#include "BMR.h"
#include <iostream>
using namespace std;
BMR::BMR()
{
}
BMR::BMR(char k, int h, int w, int a)
{
gender = k;
height = h;
weight = w;
age = a;
}
int BMR::calculateBMR()
{
int BMR;
if(this->gender == 'M')
{
BMR = 10 * weight + (6.25 * height - (5*age)) + 5;
cout << "Man: " << BMR << endl;
}
if(this->gender == 'W')
{
BMR = 10 * weight + (6.25 * height - (5*age)) - 161;
cout << "Woman: " << BMR << endl;
}
}
Program's purpose: This program's purpose it to create a basic 2d game using only the consol. The program currently only creates a moveable box with the keys "wasd". I'm planning to incorperate bullets, enemy NPC's, barriers, and much more.
Problem:
Hello, for some reason my bullet constructor class in my "Bullet.cpp" file is showing up with the error "error C2512: 'BoxClass': no appropriate default constructor available". In "Bullet.h" I have "class Bullet : protected BoxClass". Why am I getting this error?
Other Question(s):
Also how should I group my headers? They're getting to become a large cluster.
Comments:
I also realize my game loops shouldn't be in the "BoxClass.cpp". I'm going to fix that after I get my bullet class working. If I'm structuring anything else wrong or if you see an easier way of writing some of the same code just let me know.
This is my first graphical game in c++!
ConsolApplication1.cpp:
#include "stdafx.h"
#include <windows.h>
#include "BoxClass.h"
#include "ConsolWindow.h"
#include "Bullet.h"
#include <iostream>
using namespace std;
#define W_KEY 0x57
#define S_KEY 0x53
#define A_KEY 0x41
#define D_KEY 0x44
#define R_KEY 0x52
int main() {
//variable declaration/definition
int right_Wall, speed_Var;
//Consol Size/Position
int half_screen_Size = (GetSystemMetrics(SM_CXSCREEN)/2);
Set_Consol_Size(half_screen_Size, GetSystemMetrics(SM_CYSCREEN));
Position_Consol(-6, 0);
while (1) {
cout << "Enter speed of rectangle/box\nSpeed = ";
cin >> speed_Var;
BoxClass box1(4, 3);
box1.Print_Solid_Rectangle();
cout << "\n\nMove the rectangle with wasd\n\n";
//Rectangle Movement
box1.Rectangle_Movement(speed_Var);
}
//exit
return 0;
}
BoxClass.cpp:
#include "stdafx.h"
#include "BoxClass.h"
#include "ConsolWindow.h"
#include <iostream>
#include <math.h>
#include <Windows.h>
using namespace std;
#define W_KEY 0x57
#define S_KEY 0x53
#define A_KEY 0x41
#define D_KEY 0x44
#define R_KEY 0x52
#define _NOT_MOVING 0
#define _MOVING 1
//Non Moving Rectangle
void BoxClass::Print_Solid_Rectangle() {
//calc
boxSpacesWidth = (3 * recWidth) - 4;
//draw top of box
for (width = 1; width < recWidth; width += 1) {
cout << "...";
}
cout << "\n";
//draw sides
for (height = 1; height < recHeight; height += 1) {
cout << ":";
height_Count++;
for (width = 1; width < boxSpacesWidth; width += 1) {
cout << " ";
}
cout << ":\n";
}
//draw bottom
cout << ":";
for (width = 1; width < boxSpacesWidth; width += 1) {
cout << ".";
}
cout << ":\n";
}
//Moving Rectangle
void BoxClass::Print_Rectangle_Moving(int x, int y, int horizontalSpaces, int verticleSpaces) {
//calc
boxSpacesWidth = (3 * x) - 4;
rightWall = ((x-1)*3) + horizontalSpaces;
retrieveX = (ceil((((x-1)*3)/2))+1)+horizontalSpaces;
retrieveY = verticleSpaces;
cout << retrieveY<<endl;
//New Line
for (i = 1; i <= verticleSpaces; i += 1) {
cout << "\n";
}
//draw top of box
for (width = 1; width <= horizontalSpaces; width+=1) {
cout << " ";
}
for (width = 1; width < x; width += 1) {
cout << "...";
}
cout << "\n";
//draw sides
for (height = 1; height < y; height += 1) {
for (width = 1; width <= horizontalSpaces; width += 1) {
cout << " ";
}
cout << ":";
height_Count++;
for (width = 1; width < boxSpacesWidth; width += 1) {
cout << " ";
}
cout << ":\n";
}
//draw bottom
for (width = 1; width <= horizontalSpaces; width += 1) {
cout << " ";
}
cout << ":";
for (width = 1; width < boxSpacesWidth; width += 1) {
cout << ".";
}
cout << ":\n";
}
void BoxClass::Rectangle_Movement(int speed) {
speed_Var = speed;
//Rectangle Movement
while (GetAsyncKeyState(VK_ESCAPE) == false) {
if (GetAsyncKeyState(R_KEY)) {
system("CLS");
break;
}
if (GetAsyncKeyState(W_KEY)) {
if (verticleCount > 0) {
system("CLS");
verticleCount = verticleCount - speed_Var;
Print_Rectangle_Moving(recWidth, recHeight, horizontalCount, verticleCount);
}
}
if (GetAsyncKeyState(S_KEY)) {
system("CLS");
verticleCount = verticleCount + speed_Var;
Print_Rectangle_Moving(recWidth, recHeight, horizontalCount, verticleCount);
}
if (GetAsyncKeyState(A_KEY)) {
if (horizontalCount > 0) {
system("CLS");
horizontalCount = horizontalCount - (speed_Var*2);
Print_Rectangle_Moving(recWidth, recHeight, horizontalCount, verticleCount);
}
}
if (GetAsyncKeyState(D_KEY)) {
if (rightWall < 113) {
system("CLS");
horizontalCount = horizontalCount + (speed_Var*2);
Print_Rectangle_Moving(recWidth, recHeight, horizontalCount, verticleCount);
}
}
if (GetAsyncKeyState(VK_SPACE)) {
}
}
}
// constructor
BoxClass::BoxClass(int x, int y) {
//variable definition
height_Count = 1;
speed_Var = 1;
horizontalCount = 0;
verticleCount = 0;
recWidth = x;
recHeight = y;
};
BoxClass.h:
#ifndef BOXCLASS_H
#define BOXCLASS_H
class BoxClass {
unsigned short int width;
int height, i, recWidth, recHeight, rightWall;
float boxSpacesWidth, height_Count;
int width_Var, height_Var, position_Var;
int speed_Var = 1;
unsigned short int horizontalCount = 0, verticleCount = 0;
protected:
//retrieve values for bullet spawn location
int retrieveX, retrieveY;
public:
void Print_Rectangle_Moving(int x, int y, int horizontalSpaces, int verticleSpaces);
void Print_Solid_Rectangle();
void Rectangle_Movement(int speed);
// constructor
BoxClass(int x, int y);
};
#endif
Bullet.cpp:
#include "stdafx.h"
#include "Bullet.h"
#include <iostream>
#include <Windows.h>
using namespace std;
void Bullet::Bullet_Draw_Collision() {
for (int height = 1; height <= 2; height+=1) {
cout << "|\n";
}
}
Bullet::Bullet() {
}
Bullet.h:
#ifndef BULLET_H
#define BULLET_H
#include "BoxClass.h"
class Bullet : public BoxClass {
public:
void Bullet_Draw_Collision();
//constructor
Bullet();
};
#endif
ConsolWindow.cpp:
#include "stdafx.h"
#include "ConsolWindow.h"
#include <iostream>
#include <Windows.h>
using namespace std;
int Find_Consol_Size() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return columns;
}
void Set_Consol_Size(int x, int y) {
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, x, y, TRUE);
}
void Position_Consol(int x, int y) {
HWND consoleWindow = GetConsoleWindow();
SetWindowPos(consoleWindow, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
ConsolWindow.h:
#ifndef CONSOLWINDOW_H
#define CONSOLWINDOW_H
int Find_Consol_Size();
void Set_Consol_Size(int x, int y);
void Position_Consol(int x, int y);
#endif
In c++, constructing a derived type implies that both the base and derived portions of the object must be constructed. This is done by calling the base's constructor before the derived type's constructor can begin.
The default behavior is to use the base's default constructor. However you can indicate a different base constructor to use by adding it to the member initialization list and providing appropriate arguments for it.
In your case, notice that a BoxClass only has one constructor which must be provided with an x and y, but a Bullet has no such construction argument. Perhaps some default values such as zeroes or ones might be appropriate, though I suspect you will want to add those arguments to your Bullet class. Here are a few example to illustrate how you could solve the problem.
By asking for construction arguments for Bullet :
class Bullet : public BoxClass
{
public:
//constructor
Bullet(int x, int y);
};
Bullet::Bullet(int x, int y) :
BoxClass(x, y) // Indicates which BoxClass constructor to use
{}
By providing arbitrary construction arguments to BoxClass :
class Bullet : public BoxClass
{
public:
//constructor
Bullet();
};
Bullet::Bullet() :
BoxClass(1, 1)
{}
Or by adding a default constructor to BoxClass, such as by giving a default value to each of it's existing constructor's arguments :
class BoxClass
{
public:
// This constructor is now a default constructor
BoxClass(int x = 1, int y = 1);
};
class Bullet : public BoxClass
{
public:
//constructor
Bullet();
};
Bullet::Bullet() // Uses the default BoxClass constructor
{}
Which solution is best depends on your design goals, but I suspect the first one is likely to be more appropriate.
I have written a snippet that tries to use an object from a class as a function parameter, and I keep getting the error
In function 'int main()':
75:23: error: expected primary-expression before 'test'
75:35: error: expected primary-expression before 'Knight'
I am not sure how to fix this, as I am quite new to C++.
Some example code is down below:
// Example program
#include <iostream>
#include <string>
using namespace std;
//player class
class Player {
public:
//variable declaration
string name;
string classType;
int strength, perception, endurance, charisma, intelligence, agility, luck;
int id, cubes; // currency etc.
bool authority;
int inventory[20] = {};
int health = 100 + ((strength * endurance) * 2);
//sub stat functions
double getPick() {
return ((intelligence + luck) * (agility / 4)) * .01;
}
double getSneak() {
return (25 + (agility * 5)) * 0.01;
}
double getIntimidation() {
return (charisma * 10) * 0.01;
}
double getBarter() {
return getIntimidation();
}
double getScience() {
return ((intelligence * 5) / 3);
}
};
//enemys
class enemy {
public:
//var declaration
string name;
int HP;
double AC; //armor class ablity to resist hits
int DT; //dice used to attack
int eid; //id for enemys (enemy id)
int gear[2] = {}; //gear
//is the enemy alive?
int alive() {
if (HP <= 0) cout << "\nThe " << name << " is dead! ";
return false;
}
};
//fight an enemy (option 1)
int fightEnemy(Player player1, enemy enemy1) {
cout << "\n" << player1.name << " and a " << enemy1.name << "\n";
return 0;
}
int main() {
//test
Player test;
test.name = "test";
test.classType = "test";
test.strength = 3;
test.perception = 3;
test.endurance = 6;
test.charisma = 2;
test.intelligence = 6;
test.agility = 3;
test.luck = 5;
test.id = 1;
test.authority = true;
test.cubes = 500;
enemy Knight;
Knight.name = "Knight";
Knight.HP = 20;
Knight.AC = 0.2;
Knight.DT = 12;
Knight.eid = 3;
fightEnemy(Player test, enemy Knight);
return 0;
}
fightEnemy(Player test, enemy Knight);
Syntax is wrong here. You just pass the variables to the function, you are essentially declaring them again.
It should be
fightEnemy(test, Knight);
fightEnemy(Player test, enemy Knight);
to
fightEnemy(test, Knight);
The variables are already initialized.