接收错误变量的类型不完整"void"
Receiving error variable has incomplete type "void"
我正在编写一个基本的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++程序应该遵循以下代码布局。
- 项目包括
- 函数原型
- 主要功能
- 函数定义
除了需要在开始函数定义之前结束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件事会使您的示例代码无法编译干净:
- 在你重新声明函数'Menu'之前,main之后缺少了一个右括号'}',实际上是在试图声明&定义嵌套函数,这是不允许的。
- 你不是想重新声明函数'Menu',你是想定义它:函数参数列表后面的末尾分号需要删除。 您缺少函数
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;
}
函数名后面有一个分号
相关文章:
- 不能将 "void *" 类型的值分配给类型 "TCHAR" 的实体
- 不能将 "void *" 类型的值分配给类型 "RANDOMSTRUCT *" 的实体
- 在C++中将uint64_t转换为void类型的目的是什么
- 带有void类型和参数的C++11模板专用化
- 编写完整专业化以识别void类型的问题
- 非类型模板参数可以是 "void*" 类型吗?
- 如何将“void *”类型更改为“字符串”类型
- 智能感知:不能将 "void" 类型的值分配给类型 "double" 的实体
- 传递对指向 void 类型的指针的指针的引用
- 无法从 void* 类型转换(类名::)() 以键入 void*(*)(void*)
- 如何使用void类型处理递归回溯返回
- 异步返回 void 类型的未来,而不是预期类型
- std::is_same 表示 void 函数 (..) 和 void 类型
- void * 类型的参数与类型的参数不兼容
- C++:无法使用 'void' 类型的右值初始化 'void (*)()' 类型的参数
- void类型的OpenGL C++参数与void(*)()类型的参数不兼容
- 为什么我们不能声明一个 void 类型的变量?
- 如果void类型函数什么都不返回会发生什么
- 在声明后将int类型强制转换为void类型
- C++ 动态数组:不能使用 "void" 类型的值来初始化 "int" 类型的实体