第二阶段的本地函数定义非法

''SecondPhase' local function definition illegal

本文关键字:函数 定义 非法      更新时间:2023-10-16

我制作了一个基本的战斗系统,我希望在故事的不同阶段都可以使用。

  int GameStart()
{
    std::unique_ptr<blankCharacter> myCharacter;
    std::unique_ptr<blankEnemy> myEnemy;
    std::unique_ptr<Inventory> myInventory;
    std::unique_ptr<blankEnemy> myBoss;

    string name;
    int choice;
    int Battle;


    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
            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));
        break;
        case 2: //Warrior
            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));

        break;
        case 3: //Rogue
            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));   

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

在故事的开始部分之后,您可以沿着替代路径前进,但是尽管我尝试通过它们,但我似乎无法获得信息以传递到它们的路径上。

   int SecondPhase(blankCharacter, blankEnemy, string name, Inventory, int Battle, unique_ptr<blankCharacter> myCharacter, unique_ptr<blankEnemy> myEnemy, unique_ptr<Inventory> myInventory,unique_ptr<blankEnemy> myBoss) 

有谁知道如何解决问题?

>std::unique_ptr 不是可复制构造的,因此您不能按值将其传递给任何函数。尝试通过引用传递它:

int SecondPhase(std::unique_ptr<blankCharacter>& myCharacter)

有关更详细的答案,请参见此处:如何将unique_ptr参数传递给构造函数或函数?

但是我并不真正理解您的代码。例如Enemy似乎是从blankEnemy继承而来的,而名称表明blankEnemyEnemy的特例,因此继承应该是相反的(如果有的话,因为空白听起来像没有初始化)。

您不一致的命名风格也无济于事。为什么有些类型以大写字母开头,有些以小写字母开头?