当前位置: 首页 > 知识库问答 >
问题:

本地函数定义是非法的,尽管没有定义彼此内部的函数

殷学
2023-03-14

我正在尝试在C++中制作一个半复杂的基于文本的RPG。但是,它总是给我一个错误,“本地函数定义是非法的”,但是我没有在其他函数中定义任何函数,只是调用它们。代码不循环任何东西,它只是让函数互相调用。这是问题的一部分吗?下面是完整的代码,请告诉我我做错了什么。

#include <iostream>
#include <list>
#include <random>
#include "text_adventure.h"
#include <stdlib.h>
#include <time.h>

template < class Type, class Alloc = allocator<int> > class list;

using namespace std; //if you define a namespace you won't have to type out "[NAMESPACE]::[VARIABLE]" everytime.
//misc data
string choice;
int turn = 1;
int saved_turn;
int wait;
int target;
int damage_done;
int accuracy;
//ally data
string unit;
std::vector<int> unit_stats = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0 };
string knight_name = "Knight";
std::vector<int> knight = {35, 35, 10, 10, 10, 0, 7, 3, 3, 3,0 }; //HP, MaxHP, MP, MaxMP, Strength, Magic, Defense, Resistance, Speed, Luck, Defending
string wizard_name = "Wizard";
std::vector<int> wizard = { 25, 25, 40, 40, 3, 10, 2, 10, 3, 3,0 };
string cleric_name = "Cleric";
std::vector<int> cleric = { 15, 15, 30, 30, 2, 5, 7, 7, 3, 3,0 };
string thief_name = "Thief";
std::vector<int> thief = { 10, 10, 5, 5, 5, 5, 7, 7, 3, 3,0 };
//enemy data
string enemy_name;
std::vector<int> enemy = { 0,0,0,0,0,0,0,0,0,0,0 };
string enemy1_name;
std::vector<int> enemy1 = { 0,0,0,0,0,0,0,0,0,0,1 }; //HP, MaxHP, MP, MaxMP, Strength, Magic, Defense, Resistance, Speed, Luck, Type
string enemy2_name;
std::vector<int> enemy2 = { 0,0,0,0,0,0,0,0,0,0,1 };
string enemy3_name;
std::vector<int> enemy3 = { 0,0,0,0,0,0,0,0,0,0,1 };
string enemy4_name;
std::vector<int> enemy4 = { 0,0,0,0,0,0,0,0,0,0,1 };
//enemy data (2)
string minigoop_name = "Lesser Slime";
std::vector<int> minigoop = { 5, 5, 0, 0, 1, 0, 0, 0, 1, 1 }; //HP, MaxHP, MP, MaxMP, Strength, Magic, Defense, Resistance, Speed, Luck,

void calc_damage() {
    if (turn <= 4) {
        damage_done = unit_stats[4] / ((enemy[6]) / 3) + 1;
        cout << unit << " dealt " << damage_done << " points of damage to " << enemy_name << "!" << endl;
        if (target == 1) {
            enemy[0] -= damage_done;
            enemy1[0] = enemy[0];
        }
        else if (target == 2) {
            enemy[0] -= damage_done;
            enemy2[0] = enemy[0];
        }
        else if (target == 3) {
            enemy[0] -= damage_done;
            enemy3[0] = enemy[0];
        }
        else if (target == 4) {
            enemy[0] -= damage_done;
            enemy4[0] = enemy[0];
        }
        else {
            damage_done = enemy[4] / ((unit_stats[6]) / 3) + 1;
            if (target == 1) {
                knight[0] -= damage_done;
            }
            else if (target == 2) {
                wizard[0] -= damage_done;
            }
            else if (target == 3) {
                cleric[0] -= damage_done;
            }
            else if (target == 4) {
                thief[0] -= damage_done;
            }
            cout << enemy_name << " dealt " << damage_done << " points of damage to " << unit << "!" << endl;
        }

    }
}

void make_enemies() {
    if (enemy1[10] == 1)
    {
        enemy1_name = minigoop_name;
        enemy1[0] = minigoop[0];
        enemy1[1] = minigoop[1];
        enemy1[2] = minigoop[2];
        enemy1[3] = minigoop[3];
        enemy1[4] = minigoop[4];
        enemy1[5] = minigoop[5];
        enemy1[6] = minigoop[6];
        enemy1[7] = minigoop[7];
        enemy1[8] = minigoop[8];
        enemy1[9] = minigoop[9];
    }
    else if (enemy2[10] == 1)
    {
        enemy2_name = minigoop_name;
        enemy2[0] = minigoop[0];
        enemy2[1] = minigoop[1];
        enemy2[2] = minigoop[2];
        enemy2[3] = minigoop[3];
        enemy2[4] = minigoop[4];
        enemy2[5] = minigoop[5];
        enemy2[6] = minigoop[6];
        enemy2[7] = minigoop[7];
        enemy2[8] = minigoop[8];
        enemy2[9] = minigoop[9];
    }
    else if (enemy3[10] == 1)
    {
        enemy3_name = minigoop_name;
        enemy3[0] = minigoop[0];
        enemy3[1] = minigoop[1];
        enemy3[2] = minigoop[2];
        enemy3[3] = minigoop[3];
        enemy3[4] = minigoop[4];
        enemy3[5] = minigoop[5];
        enemy3[6] = minigoop[6];
        enemy3[7] = minigoop[7];
        enemy3[8] = minigoop[8];
        enemy3[9] = minigoop[9];
    }
    else if (enemy4[10] == 1)
    {
        enemy4_name = minigoop_name;
        enemy4[0] = minigoop[0];
        enemy4[1] = minigoop[1];
        enemy4[2] = minigoop[2];
        enemy4[3] = minigoop[3];
        enemy4[4] = minigoop[4];
        enemy4[5] = minigoop[5];
        enemy4[6] = minigoop[6];
        enemy4[7] = minigoop[7];
        enemy4[8] = minigoop[8];
        enemy4[9] = minigoop[9];
    }
}

void enemy_stats() {
    if (turn == 5)
    {
        enemy_name = enemy1_name;
        enemy[0] = enemy1[0];
        enemy[1] = enemy1[1];
        enemy[2] = enemy1[2];
        enemy[3] = enemy1[3];
        enemy[4] = enemy1[4];
        enemy[5] = enemy1[5];
        enemy[6] = enemy1[6];
        enemy[7] = enemy1[7];
        enemy[8] = enemy1[8];
        enemy[9] = enemy1[9];
    }
    else if (turn == 6)
    {
        enemy_name = enemy2_name;
        enemy[0] = enemy2[0];
        enemy[1] = enemy2[1];
        enemy[2] = enemy2[2];
        enemy[3] = enemy2[3];
        enemy[4] = enemy2[4];
        enemy[5] = enemy2[5];
        enemy[6] = enemy2[6];
        enemy[7] = enemy2[7];
        enemy[8] = enemy2[8];
        enemy[9] = enemy2[9];
    }
    else if (turn == 7)
    {
        enemy_name = enemy3_name;
        enemy[0] = enemy3[0];
        enemy[1] = enemy3[1];
        enemy[2] = enemy3[2];
        enemy[3] = enemy3[3];
        enemy[4] = enemy3[4];
        enemy[5] = enemy3[5];
        enemy[6] = enemy3[6];
        enemy[7] = enemy3[7];
        enemy[8] = enemy3[8];
        enemy[9] = enemy3[9];
    }
    else if (turn == 8)
    {
        enemy_name = enemy4_name;
        enemy[0] = enemy3[0];
        enemy[1] = enemy3[1];
        enemy[2] = enemy3[2];
        enemy[3] = enemy3[3];
        enemy[4] = enemy3[4];
        enemy[5] = enemy3[5];
        enemy[6] = enemy3[6];
        enemy[7] = enemy3[7];
        enemy[8] = enemy3[8];
        enemy[9] = enemy3[9];
    }
}

void reset_defense() {
    knight[10] = 0;
    wizard[10] = 0;
    cleric[10] = 0;
    thief[10] = 0;
}

void defend() {
    if (turn == 1)
    {
        knight[10] = 1;
    }
    else if (turn == 2)
    {
        wizard[10] = 1;
    }
    else if (turn == 3)
    {
        cleric[10] = 1;
    }
    else if (turn == 4)
    {
        thief[10] = 1;
    }
}

void whos_turn() {
    if (turn == 1)
    {
        unit = knight_name;
        unit_stats[0] = knight[0];
        unit_stats[1] = knight[1];
        unit_stats[2] = knight[2];
        unit_stats[3] = knight[3];
        unit_stats[4] = knight[4];
        unit_stats[5] = knight[5];
        unit_stats[6] = knight[6];
        unit_stats[7] = knight[7];
        unit_stats[8] = knight[8];
        unit_stats[9] = knight[9];
    }
    else if (turn == 2)
    {
        unit = wizard_name;
        unit_stats[0] = wizard[0];
        unit_stats[1] = wizard[1];
        unit_stats[2] = wizard[2];
        unit_stats[3] = wizard[3];
        unit_stats[4] = wizard[4];
        unit_stats[5] = wizard[5];
        unit_stats[6] = wizard[6];
        unit_stats[7] = wizard[7];
        unit_stats[8] = wizard[8];
        unit_stats[9] = wizard[9];
    }
    else if (turn == 3)
    {
        unit = cleric_name;
        unit_stats[0] = cleric[0];
        unit_stats[1] = cleric[1];
        unit_stats[2] = cleric[2];
        unit_stats[3] = cleric[3];
        unit_stats[4] = cleric[4];
        unit_stats[5] = cleric[5];
        unit_stats[6] = cleric[6];
        unit_stats[7] = cleric[7];
        unit_stats[8] = cleric[8];
        unit_stats[9] = cleric[9];
    }
    else if (turn == 4)
    {
        unit = thief_name;
        unit_stats[0] = thief[0];
        unit_stats[1] = thief[1];
        unit_stats[2] = thief[2];
        unit_stats[3] = thief[3];
        unit_stats[4] = thief[4];
        unit_stats[5] = thief[5];
        unit_stats[6] = thief[6];
        unit_stats[7] = thief[7];
        unit_stats[8] = thief[8];
        unit_stats[9] = thief[9];
    }
}

void ally_stats() {
    cout << "\n\n" << knight_name << ":\n---HP: (" << knight[0] << " / " << knight[1] << ")" << "\n---MP: (" << knight[2] << " / " << knight[3] << ")" << "\n---Strength: " << knight[4] << "\n---Magic: " << knight[5] << "\n---Defense: " << knight[6] << "\n---Resistance: " << knight[7] << "\n---Speed: " << knight[8] << "\n---Luck: " << knight[9];
    cout << "\n\n" << wizard_name << ":\n---HP: (" << wizard[0] << " / " << wizard[1] << ")" << "\n---MP: (" << wizard[2] << " / " << wizard[3] << ")" << "\n---Strength: " << wizard[4] << "\n---Magic: " << wizard[5] << "\n---Defense: " << wizard[6] << "\n---Resistance: " << wizard[7] << "\n---Speed: " << wizard[8] << "\n---Luck: " << wizard[9];
    cout << "\n\n" << cleric_name << ":\n---HP: (" << cleric[0] << " / " << cleric[1] << ")" << "\n---MP: (" << cleric[2] << " / " << cleric[3] << ")" << "\n---Strength: " << cleric[4] << "\n---Magic: " << cleric[5] << "\n---Defense: " << cleric[6] << "\n---Resistance: " << cleric[7] << "\n---Speed: " << cleric[8] << "\n---Luck: " << cleric[9];
    cout << "\n\n" << thief_name << ":\n---HP: (" << thief[0] << " / " << thief[1] << ")" << "\n---MP: (" << thief[2] << " / " << thief[3] << ")" << "\n---Strength: " << thief[4] << "\n---Magic: " << thief[5] << "\n---Defense: " << thief[6] << "\n---Resistance: " << thief[7] << "\n---Speed: " << thief[8] << "\n---Luck: " << thief[9];
}

void enemy_turn() {
    cout << "\n\n\n" << endl;
    enemy_stats();
    if (enemy[0] > 0) {
        cout << enemy_name << " attacks ";
        target = rand() % 4;
        saved_turn = turn;
        turn = target;
        whos_turn();
        cout << unit << "!" << endl;
        calc_damage();

    }
    turn = saved_turn;
    turn += 1;
    if (turn >= 8) {
        turn = 1;
        ally_turn();
    }
}

void ally_turn() {
    whos_turn();
    cout << unit <<"'s turn!" << endl;
    cout << "What will " << unit << " do?\n---1. Attack\n---2. Skill\n---3. Item\n---4. Defend\n---5. Check\n---6. Run!(like cowards)\n---7. View Allies' Stats" << endl;
    cin >> choice;
    if (choice == "7")
    {
        ally_stats();
        cout << "\n\n" << endl;
        ally_turn();
    }
    else
    {
        if (choice == "1")
        {
            cout << "Attack who?" << endl;
            cin >> choice;
        }
        else if (choice == "2")
        {
            cout << "Use which skill?" << endl;
            cin >> choice;
        }
        else if (choice == "3")
        {
            cout << "Use which item?" << endl;
            cin >> choice;
            cout << "On who?" << endl;
            cin >> choice;
        }
        else if (choice == "4")
        {
            cout << unit << " will take 50% less damage this turn!" << endl;

        }
        else if (choice == "5")
        {
            cout << "Get information on which enemy?" << endl;
            cin >> choice;
        }
        else if (choice == "6")
        {
            cout << "You're really running away?\n---1. Yes\n---any. No" << endl;
            cin >> choice;
            if (choice == "1")
            {
                cout << "You're really, REALLY running away!?\n---1. REALLY really\n---any. no" << endl;
                cin >> choice;
                if (choice == "1")
                {
                    turn -= 1;
                    cout << "\n\n" << endl;
                    ally_turn();
                }
            }
            else
            {
                cout << "\n\n" << endl;
                ally_turn();
            }
        }
        else
        {
            cout << choice << " is not a valid choice!\n\n" << endl;
            ally_turn();
        }
        turn += 1;
    }
    if (turn <= 4)
    {
        cout << "\n\n" << endl;
        ally_turn();
    }
    else
    {
        enemy_turn();
    }
}

int main()
{
    cout << "     .\n    /#\\\n   /###\\\n   #####\n   #####\n   #####\n   #####\n   #####\n   #####\n   #####\n   #####\n   #####\n   #####\n   #####\n #########\n   -----\n   #####\n   -----\n   #####\n  #######         " << endl; //ascii art of a sword
    cout << "         .\n    .       .\n     .          .\n         /##\n   .    ###/\n    .   ###\\ /#\n        #######\n  .      \\###/\n     .         .\n            .\n        .\n    .            .\n        .   .\n .  .         .\n    .           .\n          .\n.   .    .\n    .          .\n    .  .     .   ." << endl; //ascii art of the night sky
    make_enemies();
    ally_turn();
}

共有1个答案

章盛
2023-03-14

我已经解决了这个问题。发生的情况是,函数缺少标识符,编译器错误地认为函数是在彼此内部定义的。

 类似资料:
  • Lua 使用关键字 function 定义函数,语法如下: function function_name (arc) -- arc 表示参数列表,函数的参数列表可以为空 -- body end 上面的语法定义了一个全局函数,名为 function_name. 全局函数本质上就是函数类型的值赋给了一个全局变量,即上面的语法等价于 function_name = function (arc)

  • 在 Python 中,定义函数使用 def 语句。一个函数主要由三部分构成: 函数名 函数参数 函数返回值 让我们看一个简单的例子: def hello(name): return name >>> r = hello('ethan') >>> r 'ethan' 在上面,我们定义了一个函数。函数名是 hello;函数有一个参数,参数名是 name;函数有一个返回值,name。 我们也可以

  • 我只是想学习Angular 2(特别是Angular 8),就我的一生而言,我不明白为什么类变量在类函数中是“未定义”的,但如果我用ES6风格编写函数,它是可以访问的。 我尝试在构造函数中设置,但这没有意义。 一旦调用HandleClickStart,每1.5秒输出一次NaN。为什么????我本以为是1 2 3...... 通过这种方式实现handleClickStart,可以获得预期的结果: 但

  • 简介 在前面的章节中,我已经讲解了: 如何安装MIT-Scheme; Scheme解释器是如何对S-表达式求值; 基本的表操作; 在本章中,我会讲解如何自定义函数。由于Sheme是函数式编程语言,你需要通过编写小型函数来构造程序。因此,明白如何构造并组合这些函数对掌握Scheme尤为关键。在前端定义函数非常不便,因此我们通常需要在文本编辑器中编辑好代码,并在解释器中加载它们。 如何定义简单函数并加

  • 在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回。 我们以自定义一个求绝对值的my_abs函数为例: def my_abs(x): if x >= 0: return x else: return -x 请自行测试并调用my_abs看看返回结果

  • 在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回。 我们以自定义一个求绝对值的my_abs函数为例: # -*- coding: utf-8 -*- ---- def my_abs(x): if x >= 0: return x else: re