So I'm trying to pass a class as a parameter to another class (in a different header) and keep getting an error which I can't seem to debug.
Error:
'wanderingSoul::wanderingSoul(wanderingSoul &&)': cannot convert argument 1 from 'player' to 'const wanderingSoul &'
The main problem lies in the game.cpp file, below "//Battle Test" where I call "wanderingSoul wanderS(main)".
'player' is a class, and wandering soul is one as well, and the thing I don't get is how to properly pass/reference the player class since it works well when passing into functions, but not for this class constructor.
- game.cpp
#include <iostream> #include <string> #include <ctime> #include <cstdlib> #include "player.h" #include "npc.h" #include "enemy.h" #include "map.h" #include "items.h" #include "dialog.h" /* To Do List * Multiple chars per battle if in party. * Add inventory & powers * Add battles * Finish "story" & dialogues * Make map * Add dialog trees (instead of if/else & switch/case) * Add relation between characters using weighted graph */ void battle(player plyr, enemy nme) { std::cout << "You have entered a battle with " << nme.name << std::endl; while (!plyr.dead() && !nme.dead()) { } } int roll() { return rand() % 12 + 1; } int main() { srand(time(NULL)); std::string playerName; char optionChoice; int dialogChoice; npc Lexa("Lexa", 80, 80); BEGIN_GAME: system("cls"); std::cout << Lexa.nameText() << "Hey... Hey, are you awake now?" << std::endl; std::cout << Lexa.nameText() << "I was starting to get worried. You've been out for quite some time. Do you remember your name?" << std::endl; INSERT_NAME: std::cout << "(Name): "; std::cin >> playerName; std::cout << Lexa.nameText() << "Huh. " << playerName << ". Is that right? (y/n)" << std::endl; std::cin >> optionChoice; if (optionChoice != 'y' && optionChoice != 'Y') { //system("cls"); std::cout << Lexa.nameText() << "Please try to remember, I need to make sure you're stable. " << std::endl; goto INSERT_NAME; } player main(playerName); INTRODUCTION: system("cls"); //Print location initInfo(main, Lexa); //BATTLE TEST wanderingSoul wanderS(main); battle(main, wanderS.ws); //TRAVEL TEST system("pause"); return 0; }
- enemy.h
#pragma once #include <vector> #include "powers.h" #include "items.h" #include "player.h" //Need to include map? class enemy { private: /**Stat Properties********* * Health: 0->100 * 0 = Death/GameOver * Level: 1->100 * Attack: 1->10 * Defense: 1->10 * Mana: 0->100 * Affiliation: -100->100 **************************/ /* * Add inventory * -list of pointers to inventory items * Add powers * -list of pointers to powers */ class currPowers //Keeps track of current powers have { private: public: }; public: unsigned int health, level, attackPWR, defensePWR, mana; int affiliation; std::string name; enemy() { health = 100; level = 1; attackPWR = 1; defensePWR = 1; mana = 100; affiliation = 0; } enemy(std::string t_name) { name = t_name; health = 100; level = 1; attackPWR = 1; defensePWR = 1; mana = 100; affiliation = 0; } /* void attack(player plyr, power pwr) { } */ void defend() { } bool dead() { if (health == 0 || level == 0) { if (level == 0) { std::cout << name << " had a heart attack." << std::endl; } else { std::cout << name << " has been defeated. Hoorah!" << std::endl; } return true; } return false; } std::string nameText() { return name + ": "; } }; /* ENEMY LIST * STD/COMMON * Wandering Soul * Red Royal Soldier * Wolf (Pack) * B.A.R * Disciples * UNCOMMON * Tech Soldier * Banished Mage * Tech Hound * Dark Mage * RARE * Cyber Cyclops * Hellhound * Elite Androids * Follower of The Void * Banshee * BOSS * Mantis * Mary S. * The Summoner * NOX-322 * The Void */ class wanderingSoul { public: /* Name: Wandering Soul Health: 75/100 Level: |player level| - 1, level > 0. Attack: Defense: Mana: Affiliation: Powers: */ enemy ws; int diceRoll; wanderingSoul(player plyr) { ws.name = "Wandering Soul"; diceRoll = roll(); //Rolling for health if (diceRoll == 0) { ws.health = 50; } else if (diceRoll > 6) { ws.health = 100; } else { ws.level = 75; } diceRoll = roll(); //Rolling for level if (diceRoll == 0) { ws.level = plyr.level - 1; } else if (diceRoll > 6) { ws.level = plyr.level + 1; } else { ws.level = plyr.level; } } };
- player.h
#pragma once #include <vector> #include "powers.h" #include "items.h" #include "enemy.h" //Need to include map? class player { private: /**Stat Properties********* * Health: 0->100 * 0 = Death/GameOver * Level: 1->100 * Attack: 1->10 * Defense: 1->10 * Mana: 0->100 * Affiliation: -100->100 **************************/ //Setting everything outside private for now due to needing to access variables in other classes. /* * Add inventory * -list of pointers to inventory items * Add powers * -list of pointers to powers */ class currPowers //Keeps track of current powers have { private: public: }; public: unsigned int health, level, attackPWR, defensePWR, mana; int affiliation; std::string name; player(std::string t_name) { name = t_name; health = 100; level = 1; attackPWR = 1; defensePWR = 1; mana = 100; affiliation = 0; } void incAffiliation(bool x) { if (x == true) { affiliation += 5; } else { affiliation -= 5; } } void attack(enemy nme, power pwr) { } void defend() { } bool dead() { if (health == 0) { std::cout << "You are dead." << std::endl; return true; } return false; } std::string nameText() { return name + ": "; } };
Sorry if the code is bad with many uneeded lines, I'm planning on cleaning it up soon but wanted to see if I can get a battle working with main character and enemy, although I can't tell what is the problem. The reason I am trying to pass the player (main character) class as parameter is because I'm basing the enemy's stats off of the player's stats and I'm not the best with pointers and derefencing stuff so that's most likely a problem... Anyways, sorry for the long, eh, code but this is the first time I'm trying to make a small game through c++ and am trying to learn new things. (Well I know about classes and structures, but this is my first time using c++ for non-school/non-datastructure-implementation work)
UPDATE: Erased the game.cpp portion that was messing up and see that a problem is a 'syntax error: inditifier 'player'", so I'm trying to see how to fix that.