C++函数调用不起作用

C++ function calling does not work

本文关键字:不起作用 函数调用 C++      更新时间:2023-10-16

在下面的代码中:返回值可能关闭,.主要问题是我得到一个错误,如果你需要确切的错误,它不会正确调用,只是评论说,我会在其他函数中也需要返回

int menu()
{
    system("cls");
    cout << "1 for gamemodes 2 for class editor (class editor not yet   
    made coming soon)" << endl;                           
    system("pause>nul");
    if (GetAsyncKeyState(VK_NUMPAD1))
    {
        gamemodes();
    }
    return 0;
}
int gamemodes()
{
    system("cls");
    cout << "pick a gamemode" << endl;
    cout << "1 for team death match" << endl;
    cout << "rest coming soon!!!!" << endl;
    system("pause>nul");
    if (GetAsyncKeyState(VK_NUMPAD1))
    {
        map_picker();
    }
    return 0;
}
int map_picker()
{
    while (go = true)
    { 
        system("cls");
        cout << "pick a map" << endl;
        cout << "1 for town" << endl;
        system("pause>nul");
        if (GetAsyncKeyState(VK_NUMPAD1))
        {
            town_load();
        }
        return 0;
}
int town_load()
{
    return 0;
}
以下是

需要修复的错误:

 'gamemodes': identifier not found,
更新

对此代码的任何更新都将在此处引起来"如果我在 main 之前声明它们,它们会在被调用之前执行/使用

">

main 中使用它之前,您还没有声明gamemodes。在 main 之前添加声明。

int gamemodes();
int main()
{
   ...
}

对OP评论的回应

宣言

int gamemodes();

不会导致函数调用。声明的存在是为了允许使用该函数。

该函数将在您拥有的代码块中main调用:

if (GetAsyncKeyState(VK_NUMPAD1))
{
   // This is where the function gets called.
   gamemodes();
}

原始示例中有许多未声明的变量和函数。

当你在main function之后声明一个函数,然后尝试在这个main function中使用它时,你需要定义一个函数原型,该原型是声明为指定函数名称和类型签名的函数

因此,对于您的代码:

您需要先为函数GetAsyncKeyState或原型添加代码,然后再为该函数添加代码。

与函数int gamemodes()map_picker()相同

您还必须声明布尔变量和VK_NUMPAD1变量go

您还必须添加主函数

这是一个没有编译错误的代码;但你必须为代码中使用的不同函数添加代码。

#include <iostream>
using namespace std;
int town_load();
int gamemodes();
int VK_NUMPAD1 = 1;
bool go = true;
int map_picker();
int GetAsyncKeyState(int VK_NUMPAD1) {
    return 1;
}
int menu()
{
    system("cls");
    cout << "1 for gamemodes 2 for class editor (class editor not yet made coming soon)" << endl;                           
        system("pause>nul");
    if (GetAsyncKeyState(VK_NUMPAD1))
    {
        gamemodes();
    }
    return 0;
}
int gamemodes()
{
    system("cls");
    cout << "pick a gamemode" << endl;
    cout << "1 for team death match" << endl;
    cout << "rest coming soon!!!!" << endl;
    system("pause>nul");
    if (GetAsyncKeyState(VK_NUMPAD1))
    {
        map_picker();
    }
    return 0;
}
int map_picker()
{
    while (go = true)
    {
        system("cls");
        cout << "pick a map" << endl;
        cout << "1 for town" << endl;
        system("pause>nul");
        if (GetAsyncKeyState(VK_NUMPAD1))
        {
            town_load();
        }
        return 0;
    }
}
int town_load() {
    return 0;
}
int main() {
    return 0;
}