C++ 接收错误变量具有不完整的类型“void”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16848150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:41:40  来源:igfitidea点击:

Receiving error variable has incomplete type "void"

c++variablestypesvoid

提问by user2438644

I'm coding a basic C++ program to calculate the length and slope of a line. The user enters a set of x and y coordinate points and the program then displays a menu asking the user if he/she would like to calculate only the slope, only the length, or both the slope and the length. However, I'm getting an error on my void Menu function that states that the variable has an incomplete type "void". My code as it stands now is below.

我正在编写一个基本的 C++ 程序来计算一条线的长度和斜率。用户输入一组 x 和 y 坐标点,然后程序会显示一个菜单,询问用户他/她是否只想计算斜率,仅计算长度,或同时计算斜率和长度。但是,我的 void Menu 函数出现错误,指出该变量具有不完整的类型“void”。我现在的代码如下。

#include <iostream>
#include <cmath>

void Menu (int& MenuNum);
void CalculateSlope (int& X1, int& X2, int& Y1, int& Y2);
void CalculateLength (int& X1, int& X2, int& Y1, int& Y2);

using namespace std;

int main(int argc, const char * argv[])
{

    int X1;
    int X2;
    int Y1;
    int Y2;
    int MenuNum;
    //Ask User for Points

    cout << "Enter points (X1,Y1) and (X2,Y2) for the line." << endl;
    cout << " " << endl;
    cout << "X1:" << endl;
    cin >> X1;
    cout << "Y1:" << endl;
    cin >> Y1;
    cout << "X2:" << endl;
    cin >> X2;
    cout << "Y2:" << endl;
    cin >> Y2;

    cout << "Points entered are" 
         << " : " << X1 << "," 
         << Y1 << " and " 
         << X2 << "," << Y2 << endl;
    cout << "                  "<< endl;

    //Menu

    void Menu (MenuNum);
    {
        cout << "To calculate the slope of the line, enter 1 " << endl;
        cout << "To calculate the length of the line, enter 2" << endl;
        cout << "To calculate the length and slope of the line, enter 3" << endl;
        cin >> MenuNum;

    }

Also, if you could give some guidance on how to call the slope and length calculating functions from the Menu function, that would be great.

此外,如果您能就如何从 Menu 函数调用斜率和长度计算函数提供一些指导,那就太好了。

Thanks!

谢谢!

回答by larrylampco

First, the reason that you are seeing the error "incomplete type void" is because you have a semicolon that is essentially ending the function definition of your Menu function. In layman's terms, you have not completely finished defining your function.

首先,您看到错误“incomplete type void”的原因是您有一个分号,它基本上结束了您的 Menu 函数的函数定义。通俗地说,你还没有完全定义你的函数。

Second, by convention, a simple C++ program like you have written should follow the following code layout.

其次,按照惯例,像您编写的简单 C++ 程序应遵循以下代码布局。

  1. Program Includes
  2. Function Prototypes
  3. Main Function
  4. Function Definitions
  1. 计划包括
  2. 函数原型
  3. 主功能
  4. 功能定义

You have the correct ordering of your program except that you need to end the main function before beginning your function definitions.

除了需要在开始函数定义之前结束主函数之外,您的程序顺序是正确的。

So you should have:

所以你应该有:

main()
{
  ...
}//End main function

void menu()
{
  ...
}

Another thing I notice, is that the parameters you have given your main function are usually used if you are going to be taking input from the command line. Since you are asking for input from the user in your program, you should change the way you have declared your main function.

我注意到的另一件事是,如果您要从命令行获取输入,通常会使用您为 main 函数提供的参数。由于您在程序中要求用户输入,因此您应该更改声明 main 函数的方式。

Instead of using

而不是使用

int main(int argc, const char * argv[])
{
  ...
}

Declare it like this

像这样声明

int main()
{
  ...
}

Before I can answer your last question, you need to build functionality to handle the users input. This can be done with a switch case statement. If you need information about using the switch case statement in C++, you can find a great explanation at http://www.cplusplus.com/doc/tutorial/control/

在我回答您的最后一个问题之前,您需要构建功能来处理用户输入。这可以通过 switch case 语句来完成。如果您需要有关在 C++ 中使用 switch case 语句的信息,您可以在http://www.cplusplus.com/doc/tutorial/control/ 上找到一个很好的解释

One way that you could implement this switch statement is to place the calls to the functions to calculate the slope and length of a line in your switch case. You will need to change one thing about the parameters of your menu function if you do it this way. You will need to pass the values of the coordinates to the menu function as well. For example,

您可以实现此 switch 语句的一种方法是调用函数来计算 switch 案例中直线的斜率和长度。如果您这样做,您将需要更改有关菜单功能参数的一件事。您还需要将坐标值传递给菜单功能。例如,

void Menu (MenuNum, X1, X2, Y1, Y2)
{
    cout << "To calculate the slope of the line, enter 1 " << endl;
    cout << "To calculate the length of the line, enter 2" << endl;
    cout << "To calculate the length and slope of the line, enter 3" << endl;
    cin >> MenuNum;

    switch(MenuNume)
    {
      case 1:
      {
        //call calculate slope function
        break;
      }
      case 2:
      {
        //call calculate length function
        break;
      }
      case 3:
      {
        //call both calculate slope and calculate length functions
        break;
      }
      default:
      {
         cout << "Please enter a correct value." << endl;
      }
    }

}

I think that answers all of your questions. Hope this helps!

我认为这回答了你所有的问题。希望这可以帮助!

回答by Nathan Ernst

Without other commentary, there are 3 things keeping your example code from compiling cleanly:

如果没有其他评论,有 3 件事会阻止您的示例代码干净利落地编译:

  1. You're missing a closing brace '}' after main, before you re-declare the function 'Menu', effectively attempting to declare & define a nested function, which is not allowed.
  2. You didn't mean to re-declare the function 'Menu', you meant to define it: the trailing semi-colon after the function parameter list needs to be removed.
  3. You're missing the type specification for the function Menu. It was declared void Menu(int& MenuItem), but you define it as void Menu(MenuItem). The type needs to be present at the definition.
  1. 在重新声明函数 'Menu' 之前,在 main 之后缺少右大括号 '}',有效地尝试声明和定义嵌套函数,这是不允许的。
  2. 您并不是要重新声明函数“Menu”,而是要定义它:需要删除函数参数列表之后的尾随分号。
  3. 您缺少函数的类型规范Menu。它已声明void Menu(int& MenuItem),但您将其定义为void Menu(MenuItem). 类型需要出现在定义中。

Code that compiles cleanly (but not tested) is:

干净编译(但未测试)的代码是:

#include <iostream>
#include <cmath>

void Menu (int& MenuNum);
void CalculateSlope (int& X1, int& X2, int& Y1, int& Y2);
void CalculateLength (int& X1, int& X2, int& Y1, int& Y2);

using namespace std;

int main(int argc, const char * argv[])
{

        int X1;
        int X2;
        int Y1;
        int Y2;
        int MenuNum;
        //Ask User for Points

        cout << "Enter points (X1,Y1) and (X2,Y2) for the line." << endl;
        cout << " " << endl;
        cout << "X1:" << endl;
        cin >> X1;
        cout << "Y1:" << endl;
        cin >> Y1;
        cout << "X2:" << endl;
        cin >> X2;
        cout << "Y2:" << endl;
        cin >> Y2;

        cout << "Points entered are"
                << " : " << X1 << ","
                << Y1 << " and "
                << X2 << "," << Y2 << endl;
        cout << "                  "<< endl;
}

//Menu

void Menu (int& MenuNum)
{
        cout << "To calculate the slope of the line, enter 1 " << endl;
        cout << "To calculate the length of the line, enter 2" << endl;
        cout << "To calculate the length and slope of the line, enter 3" << endl;
        cin >> MenuNum;

}

回答by user2089851

You have a semi colon on the end of your function name

您的函数名称末尾有一个分号