Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving error variable has incomplete type "void"

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.

#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.

Thanks!

like image 443
user2438644 Avatar asked May 31 '13 00:05

user2438644


2 Answers

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.

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

  1. Program Includes
  2. Function Prototypes
  3. Main Function
  4. Function Definitions

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.

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/

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,

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!

like image 147
larrylampco Avatar answered Nov 03 '22 17:11

larrylampco


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

  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.

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;

}
like image 30
Nathan Ernst Avatar answered Nov 03 '22 19:11

Nathan Ernst