简体   繁体   English

错误:“]”标记之前的预期主表达式。 需要帮助 function 定义和调用

[英]Error: expected primary-expression before ']' token. Need assitance with function definition and calling

This is my first semester of computer science, and I need help with my first project.这是我计算机科学的第一个学期,我的第一个项目需要帮助。 So far, it's still a mess, and I am mainly doing test cases to ensure basic things like my functions work.到目前为止,它仍然是一团糟,我主要是在做测试用例,以确保像我的函数这样的基本东西可以正常工作。

The goal of the project is to ask the user how many robots they want to make, name them, then they can use the robot's name (their unique identifier) to move the robots along an XY axis, which is really just the program adding or subtracting to a .Xvalue or .Yvalue .该项目的目标是询问用户他们想要制作多少个机器人,给它们命名,然后他们可以使用机器人的名称(它们的唯一标识符)沿 XY 轴移动机器人,这实际上只是程序添加或减去.Xvalue.Yvalue

My MenuFunction works, but I am having trouble with the MoveFunction .我的MenuFunction有效,但我在使用MoveFunction时遇到了问题。 Basically, I want the MoveFunction to ask the user which robot they want to use, go through the RobotArray , and print the Robot's name once found.基本上,我希望MoveFunction通过 RobotArray 询问用户他们想要使用哪个机器人, RobotArray ,并在找到后打印机器人的名称。

Again, this is just a test case so I can better understand the coding.同样,这只是一个测试用例,因此我可以更好地理解编码。 Right now, I am getting two errors:现在,我收到两个错误:

main.cpp:42:33: error: expected primary-expression before ‘]’ token
   42 |  string MoveFunction(RobotArray[], robotName, NumberOfRobots);

main.cpp:62:16: error: no match for call to ‘(std::string {aka std::__cxx11::basic_string}) ()’
   62 |   MoveFunction();

I don't know what to do for the first error, but I think the latter is due to my not having any objects in the function call, but I wouldn't know what to put in there anyway.我不知道第一个错误该怎么办,但我认为后者是因为我在 function 调用中没有任何对象,但我不知道该放什么。

My complete code is below:我的完整代码如下:

#include <iostream>
using namespace std;

struct userRobot
{
    string name;
    int Xvalue = 0;
    int Yvalue = 0;
};

void MenuFunction()
{
    cout << "Welcome to MultiRobo Guider." << endl;
    cout << "Please select:" << endl;
    cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}

int main()
{

    int NumberOfRobots;
    string robotName;
    cout << "Enter the number of robots" << endl;
    cin >> NumberOfRobots;
    cout << endl << "Enter their name(s)" << endl;
    userRobot RobotArray[NumberOfRobots];

    for (int i = 0; i < NumberOfRobots; i++)
    {
        cin >> robotName;
        RobotArray[i].name = robotName;
    }

    cout << endl;

    for (int j = 0; j < NumberOfRobots; j++)
    {
        cout << RobotArray[j].name << "'s position is ";
        cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
    }

    string MoveFunction(RobotArray[], robotName, NumberOfRobots);
    {
        cin >> robotName;
        for (int k = 0; k < NumberOfRobots; k++)
        {
            if (robotName == RobotArray[k].name)
            {
                cout << RobotArray[k].name;
            }
        }
    }

    MenuFunction();
    char input;
    cin >> input;

    if (input == 'm')
    {
        cout << "Which robot would you like to move?";
        MoveFunction();
    }
    else if (input == 'd')
    {
        cout << "distance";
    }
    else if (input == 'q')
    {
        cout << "quit";
    }
}

First off, userRobot RobotArray[NumberOfRobots];首先, userRobot RobotArray[NumberOfRobots]; is a variable-length array , which is a non-standard extension supported by only a few compilers.是一个变长数组,它是一个非标准的扩展,只有少数编译器支持。 To create an array whose size is not known until runtime, you should use new[] instead, or better std::vector .要创建一个直到运行时才知道大小的数组,您应该使用new[]代替,或者更好的std::vector

That said, your main issue is that you are trying to define your MoveFunction() function inside of your main() function, which is not allowed.也就是说,您的主要问题是您试图在 main() function内部定义MoveFunction() main() function,这是不允许的。 But, even if it were, you are not declaring it correctly.但是,即使是这样,您也没有正确声明它。 It has an erroneous ;它有一个错误的; on it.在上面。 And RobotArray , robotName , and NumberOfRobots are not types, but variables.而且RobotArrayrobotNameNumberOfRobots不是类型,而是变量。 Like a variable, a function parameter always starts with a type.与变量一样,function 参数始终以类型开头。 There are no untyped parameters/variables in C++. C++ 中没有无类型的参数/变量。

You need to either:您需要:

  • move MoveFunction() outside of main() , and fix its declaration to use proper types, eg:MoveFunction() main() ,并修复其声明以使用正确的类型,例如:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct userRobot
{
    string name;
    int Xvalue = 0;
    int Yvalue = 0;
};

void MenuFunction()
{
    cout << "Welcome to MultiRobo Guider." << endl;
    cout << "Please select:" << endl;
    cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}

void MoveFunction(userRobot RobotArray[], int NumberOfRobots)
{
    cout << "Which robot would you like to move?";

    string robotName;
    cin >> robotName;

    for (int k = 0; k < NumberOfRobots; k++)
    {
        if (robotName == RobotArray[k].name)
        {
            cout << RobotArray[k].name << endl;
            return;
        }
    }

    cout "Robot not found" << endl;
}

int main()
{
    cout << "Enter the number of robots" << endl;

    int NumberOfRobots;
    cin >> NumberOfRobots;

    vector<userRobot> RobotArray(NumberOfRobots);

    cout << endl << "Enter their name(s)" << endl;
    for (int i = 0; i < NumberOfRobots; i++)
    {
        cin >> RobotArray[i].name;
    }
    cout << endl;

    for (int j = 0; j < NumberOfRobots; j++)
    {
        cout << RobotArray[j].name << "'s position is ";
        cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
    }

    MenuFunction();
    char input;
    cin >> input;

    if (input == 'm')
    {
        MoveFunction(RobotArray.data(), NumberOfRobots);
    }
    else if (input == 'd')
    {
        cout << "distance";
    }
    else if (input == 'q')
    {
        cout << "quit";
    }
}
  • or change MoveFunction() into a lambda, eg:或将MoveFunction()更改为 lambda,例如:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct userRobot
{
    string name;
    int Xvalue = 0;
    int Yvalue = 0;
};

void MenuFunction()
{
    cout << "Welcome to MultiRobo Guider." << endl;
    cout << "Please select:" << endl;
    cout << "m - move" << endl << "d - distance" << endl << "q - quit" << endl << endl;
}

int main()
{
    cout << "Enter the number of robots" << endl;

    int NumberOfRobots;
    cin >> NumberOfRobots;

    vector<userRobot> RobotArray(NumberOfRobots);

    cout << endl << "Enter their name(s)" << endl;
    for (int i = 0; i < NumberOfRobots; i++)
    {
        cin >> RobotArray[i].name;
    }
    cout << endl;

    for (int j = 0; j < NumberOfRobots; j++)
    {
        cout << RobotArray[j].name << "'s position is ";
        cout << "(" << RobotArray[j].Xvalue << "," << RobotArray[j].Yvalue << ")" << endl << endl;
    }

    auto MoveFunction = [&]{
        cout << "Which robot would you like to move?";

        string robotName;
        cin >> robotName;

        for (int k = 0; k < NumberOfRobots; k++)
        {
            if (robotName == RobotArray[k].name)
            {
                cout << RobotArray[k].name << endl;
                return;
            }
        }

        cout "Robot not found" << endl;
    };

    MenuFunction();
    char input;
    cin >> input;

    if (input == 'm')
    {
        MoveFunction();
    }
    else if (input == 'd')
    {
        cout << "distance";
    }
    else if (input == 'q')
    {
        cout << "quit";
    }
}

You have a semi-colon at the end of MoveFunction() definition, In C++ when you define a function the name of the function does not end with a semi-colon.在 MoveFunction() 定义的末尾有一个分号,在 C++ 中,当您定义 function 时,function 的名称不以分号结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM