如何在 c++ 中不将其显示为函数定义的情况下调用函数
How to call a function without it appearing as a function definition in c++?
#include <iostream>
#include<iomanip>
using namespace std;
//prototypes
void getData(double base,double height);
void getData(double radiusa,double radiusb);
void printData(double rec_area,double elli_area);
void getData(double rpar1,double rpar2);
void printData(double vpar1,double vpar2);
//Declare variables
double base,height, radiusa, radiusb, rec_area, elli_area,rpar1, rpar2, vpar1,vpar2;
//Declare a global constant named PI equal to 3.141592;
const double PI = 3.141592;
int main()
{
//Print on the screen “For the rectangle”
cout << "For the rectangle" << endl;
//Call void function for base and height
getData(base, height);
//Print on the screen “For the ellipse”
cout << "For the ellipse" << endl;
//Call void function for lengths
getData(radiusa, radiusb);
//Calculate the area and store the result for rectangle
rec_area = base * height;
//Calculate the area and store the result for ellipse
elli_area = PI * radiusa * radiusb;
//Call void function that receives the two areas and returns them
printData(rec_area, elli_area);
//Get both values from the keyboard and store them in rpar1 and rpar2.
getData(rpar1, rpar2);
//Format to use fixed point
printData(vpar1, vpar2);
return 0;
}
//call functions
void getData(base, height)
{
return;
}
void getData(radiusa, radiusb)
{
return;
}
void printData(rec_area, elli_area)
{
cout << "The area of the rectangle is " << rec_area << endl << endl;
cout << "The area of the ellipse is " << elli_area << endl << endl;
return;
}
void getData(rpar1, rpar2)
{
return;
}
void printData(vpar1, vpar2)
{
cout << "Please enter two lenghts: " << endl << endl;
cout << fixed << showpoint << setprecision (1);
return;
}
我一直在这里和那里弄乱代码,所以如果它看起来很丑,我很抱歉。我也是菜鸟。以下是错误:
error C2448: 'getData' : function-style initializer appears to be a function definition
error C2448: 'getData' : function-style initializer appears to be a function definition
error C2448: 'printData' : function-style initializer appears to be a function definition
error C2448: 'getData' : function-style initializer appears to be a function definition
error C2448: 'printData' : function-style initializer appears to be a function definition
首先,在 main 之上编写函数声明是编写函数的好技术。
您在代码中看到的错误是因为在编写函数的实际主体时,需要再次指定参数的类型。函数的代码应该是
//declare functions
void getData(double base, double height);
void getData2(double radiusa, double radiusb);
void printData(double rec_area, double elli_area);
void getData3(double rpar1, double rpar2);
void printData2(double vpar1, double vpar2);
int main(void)
{
//your code here
}
//write body of functions
void getData(double base, double height)
{
return;
}
void getData2(double radiusa, double radiusb)
{
return;
}
void printData(double rec_area, double elli_area)
{
cout << "The area of the rectangle is " << rec_area << endl << endl;
cout << "The area of the ellipse is " << elli_area << endl << endl;
return;
}
void getData3(double rpar1, double rpar2)
{
return;
}
void printData2(double vpar1, double vpar2)
{
cout << "Please enter two lenghts: " << endl << endl;
cout << fixed << showpoint << setprecision (1);
return;
}
编辑:哦,roeland是对的,当你重载函数时,它们的参数需要有所不同,否则它们看起来与编译器相同。
相关文章:
- 不同翻译单元中不可重载的非内联函数定义
- Visual Studio中的函数声明和函数定义问题
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 具有enable_if外部类原型的模板类构造函数定义
- 类的前向声明之后的类成员函数定义,在类声明之前
- 为函数定义符号不明确的指针参数
- C++模板专用化 - 无法匹配函数定义
- 错误:在第 6 行'{'标记之前,此处不允许使用函数定义
- 找不到 #define 的函数定义
- 根据类型特征更改函数定义?
- 将抽象基类中的所有纯虚函数定义为 varaidaic 模板
- 命名空间更改函数定义
- "Type&"与C++函数定义中的"Type*"
- C++:为什么允许在另一个函数中声明函数,而不允许在函数定义中声明?
- 如何从 C++ 中的现有模板函数定义新函数
- 私有在函数定义/实现的返回值范围内是什么意思 (c++)?
- 越界成员函数定义是否需要一个完全限定的类名,直到全局范围
- 为什么c++允许成员函数定义中实例的私有成员访问
- Qt基类函数定义
- C++函数定义中参数列表后面额外一对括号的含义