gpt4 book ai didi

c++ - 无法从 std::shared_ptr <_Ty> 转换参数 1 (??)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:28 25 4
gpt4 key购买 nike

在完成我的游戏原型(prototype)时,我遇到了这个错误,我以前从未见过。我试图将两个 .cpp 文件链接在一起,这个:

    #include <iostream>
#include "mage.h"
#include "Warrior.h"
#include "Rogue.h"
#include "CharacterType.h"
#include <memory>
#include <string>
#include "Inventory.h"
#include "blankEnemy.h"
#include "Enemy.h"
#include "Boss.h"
#include <cstdlib>
#include <ctime>
#include "DeathMenu.h"
#include "GameStart.h"
#include "FirstPhase.h"
#include <Windows.h>
#include "SecondPhase.h"
#include "PhaseOnePT2.h"
using namespace std;

int FirstPhase(blankCharacter* myCharacter, blankEnemy* myEnemy, Inventory* myInventory, blankEnemy* myBoss)
{
//code
}

为此: int 游戏开始() {

    std::shared_ptr<blankCharacter> myCharacter; 
std::unique_ptr<blankEnemy> myEnemy;
std::unique_ptr<Inventory> myInventory;
std::unique_ptr<blankEnemy> myBoss;




string name;
int choice;

cout << "______________________________________________________________________________" << endl;
cout << "______________________________________________________________________________" << endl;
cout << "Your journey has only just begun" << endl;
cout << "______________________________________________________________________________" << endl;
cout << "______________________________________________________________________________" << endl;

cout << " Please enter player name." << endl << endl;
cin >> name;
system("cls");


cout << "______________________________________________________________________________" << endl;
cout << "______________________________________________________________________________" << endl;
cout << "Please select fighting class." << endl << endl;
cout <<" 1 - Mage, although not the physically strongest, mages offer a healing role" << endl;
cout <<" 2 - Warrior, the most balanced of all the classes, excelling in durability." << endl;
cout <<" 3 - Rogue, for players who want to take a more creative approach to battle." << endl;
cout << "______________________________________________________________________________" << endl;
cout << "______________________________________________________________________________" << endl;
cin >> choice;




switch(choice)
{
case 1: //Mage
Beep(262,500);
myCharacter = std::unique_ptr<blankCharacter>(new Mage(20,40,60,70,100,180,60));
myInventory = std::unique_ptr<Inventory>(new Inventory(3, 3,3));
myEnemy = std::unique_ptr<blankEnemy>(new Enemy(150, 60, 150));
myBoss = std::unique_ptr<blankEnemy>(new Enemy(200, 200, 200));
//choice = FirstPhase();
case 2: //Warrior
Beep(262,500);
myCharacter = std::unique_ptr<blankCharacter>(new Warrior(40,50,65,100,160,100,60));
myInventory = std::unique_ptr<Inventory>(new Inventory(3, 3, 3));
myEnemy = std::unique_ptr<blankEnemy>(new Enemy(150, 60, 150));
myBoss = std::unique_ptr<blankEnemy>(new Enemy(200, 200, 200));
choice = FirstPhase(myCharacter, myEnemy, myInventory, myBoss);
break;

case 3: //Rogue
Beep(262,500);
myCharacter = std::unique_ptr<blankCharacter>(new Rogue(30,55,70,90,160,100,100));
myInventory = std::unique_ptr<Inventory>(new Inventory(3, 3, 3));
myEnemy = std::unique_ptr<blankEnemy>(new Enemy(150,60,150));
myBoss = std::unique_ptr<blankEnemy>(new Enemy(200, 200, 200));
// choice = FirstPhase(myCharacter, myEnemy, myInventory, myBoss);
break;

default:
cout << "Please select a relevant value 1 to 3" << endl << endl;
break;
}
return 0;

};

我引用了 Firstphase();在标题中并将其放入 GameStart();

第一阶段.h:

    #include "GameStart.h"
#include <string>
#include "CharacterType.h"
#include "mage.h"
#include "Rogue.h"
#include "Warrior.h"
using namespace std;
int FirstPhase(blankCharacter* myCharacter, blankEnemy* myEnemy, Inventory* myInventory, blankEnemy* myBoss);

但是我一直收到这个错误:

错误 C2664:“FirstPhase”:无法将参数 1 从“std::shared_ptr<_Ty>”转换为“blankCharacter *”左右

  choice = FirstPhase(myCharacter, myEnemy, myInventory, myBoss);

没有从 shared_ptr<_Ty> 到 *blankCharacter 的合适转换存在'?我不知道如何解决它。

最佳答案

C++11 智能指针不提供与原始指针类型之间的自动转换(出于安全原因)。使用 get() :

choice = FirstPhase(myCharacter.get(), myEnemy.get(), myInventory.get(), myBoss.get());

更好的是,将您的函数更改为接受引用而不是指针(或者它是否正确处理传入的空值?)并且只是在调用站点取消引用指针。

关于c++ - 无法从 std::shared_ptr <_Ty> 转换参数 1 (??),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21044719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com