试图将class作为参数传递给不同的类构造函数,但我正在进行"转换"错误

时间:2018-04-18 01:54:08

标签: c++ class oop c++11

所以我试图将一个类作为参数传递给另一个类(在不同的标题中)并继续得到一个我似乎无法调试的错误。

错误:

  

' wanderingSoul :: wanderingSoul(wanderingSoul&&&)':不能转换来自' player'到#const wanderingSoul&'

主要问题在于game.cpp文件,在" // Battle Test"在哪里我打电话"游荡灵魂游荡(主要)"。

'播放'是一个类,流浪的灵魂也是一个,我不能得到的是如何正确传递/引用播放器类,因为它在传递给函数时效果很好,但不适用于这个类构造函数。

  • 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 + ": ";
  }

};

很抱歉,如果代码很糟糕,有很多不合情理的线路,我很快就打算清理它,但想知道我是否可以与主角和敌人进行战斗,虽然我无法分辨问题是什么。我试图将玩家(主角)类作为参数传递的原因是因为我将敌人的统计数据从玩家的统计数据中移除而且我并不是最好的指针和derefencing东西,这很可能是一个问题...... 无论如何,对于长的,呃代码感到抱歉,但这是我第一次尝试通过c ++制作一个小游戏并尝试学习新东西。 (我知道类和结构,但这是我第一次使用c ++进行非学校/非数据结构实现工作)

更新 :删除了搞乱的game.cpp部分,发现问题是语法错误:inditifier&#39; player&# 39;&#34;,所以我试图看看如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

此处的问题是,您enemy.h中包含player.hplayer.h中包含enemy.h。 然后,在game.cpp中添加它们。这是Circular dependency的情况。 当您尝试编译您没有共享的代码时,必定会有更多错误。

无论如何,您可以使用forward declaration来避免此问题。

一些附注:

由于您正在开发游戏,因此您需要了解有关inheritancecomposition的更多信息。继承是 IS-A 关系的情况,另一方面,组合是 HAS-A 的情况。

所以在这里你所做的事情在设计方面是不对的,游荡灵魂敌人,所以不是在wanderingSoul类内部创造一个敌人,而是固有的班enemy

你的敌人可以拥有多个小工具,而这些小工具可以是另一个类别,所以在这里你可以看到你有一个 HAS-A 的情况,你可以从中受益组合物。

另一件事是,PlayerEnemy在你的游戏世界中是两个独立的东西,所以将玩家传递给敌人并不是一个好主意。你需要一个游戏控制器类来控制这两个类的对象之间的交互,比如战斗或说话或者这两个类可能做的其他事情。