接收错误变量的类型不完整"void"

Receiving error variable has incomplete type "void"

本文关键字:void 类型 错误 变量      更新时间:2023-10-16

我正在编写一个基本的c++程序来计算一条线的长度和斜率。用户输入一组x和y坐标点,然后程序显示一个菜单,询问用户是只计算斜率,还是只计算长度,还是同时计算斜率和长度。然而,我在我的void菜单函数上得到一个错误,声明变量有一个不完整的类型"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;
    }

另外,如果你能给一些关于如何从菜单函数调用斜率和长度计算函数的指导,那就太好了。

谢谢!

首先,您看到错误"incomplete type void"的原因是因为您有一个分号,它实际上是菜单函数的函数定义的结束。用外行人的话来说,您还没有完全定义好函数。

第二,按照惯例,像您这样编写的简单c++程序应该遵循以下代码布局。

  1. 项目包括
  2. 函数原型
  3. 主要功能
  4. 函数定义

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

所以你应该有:

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

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

不使用

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

像这样声明

int main()
{
  ...
}

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

找到一个很好的解释。

实现这个switch语句的一种方法是调用函数来计算switch case中一条线的斜率和长度。如果这样做,您将需要更改菜单函数的参数。您还需要将坐标的值传递给菜单函数。例如

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;
      }
    }
}
我想这回答了你所有的问题。希望这对你有帮助!

如果没有其他注释,有3件事会使您的示例代码无法编译干净:

  1. 在你重新声明函数'Menu'之前,main之后缺少了一个右括号'}',实际上是在试图声明&定义嵌套函数,这是不允许的。
  2. 你不是想重新声明函数'Menu',你是想定义它:函数参数列表后面的末尾分号需要删除。
  3. 您缺少函数Menu的类型规范。它被声明为void Menu(int& MenuItem),但是你把它定义为void Menu(MenuItem)。该类型需要出现在定义中。
编译干净(但未经过测试)的代码是:
#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;
}

函数名后面有一个分号