【问题标题】:Receiving error variable has incomplete type "void"接收错误变量的类型不完整“void”
【发布时间】:2013-05-26 17:39:33
【问题描述】:

我正在编写一个基本的 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;

    }

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

谢谢!

【问题讨论】:

  • 错误信息是指哪一行?
  • @WagnerPatriota:cplusplus.com 太可怕了;不推荐!
  • C++ Primer 可能对此有很大帮助 ;)

标签: c++ variables types void


【解决方案1】:

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

  1. 在重新声明函数“Menu”之前,在 main 之后缺少右大括号“}”,实际上是在尝试声明和定义嵌套函数,这是不允许的。
  2. 您不是要重新声明函数“Menu”,而是要定义它:需要删除函数参数列表后的尾随分号。
  3. 您缺少函数Menu 的类型说明。它被声明为void Menu(int&amp; 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;

}

【讨论】:

    【解决方案2】:

    首先,您看到“incomplete type void”错误的原因是您有一个分号,它实质上是在结束 Menu 函数的函数定义。用外行的话来说,你还没有完全定义你的函数。

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

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

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

    所以你应该有:

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

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

    而不是使用

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

    这样声明

    int main()
    {
      ...
    }
    

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

    实现此 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;
          }
        }
    
    }
    

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

    【讨论】:

      【解决方案3】:

      你的函数名末尾有一个分号

      【讨论】:

        猜你喜欢
        • 2021-05-09
        • 1970-01-01
        • 2017-09-14
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-16
        • 2021-03-19
        相关资源
        最近更新 更多