c++ - 如何调用一个函数而不在 C++ 中作为函数定义出现?

标签 c++ function

#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 是对的,当您重载函数时,它们的参数需要有不同的东西,否则它们看起来与编译器相同。

关于c++ - 如何调用一个函数而不在 C++ 中作为函数定义出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33535735/

相关文章:

c++ - pthread_create() 和内存泄漏

c++ - C++ 中的抽象类(接口(interface))数组

c++ - 这个函数会泄漏内存吗?

c++ - 将类回调函数分配给结构?

c++ - OCaml 中的快速位数组

c++ - 如何阻止 Flex 为每条规则将空格输出到标准输出

javascript - onclick() 具有多个功能,仅执行第一个

c++ - 头文件中的类

c++ - 函数必须只有一个参数

c - 弹丸运动程序 : What's wrong with this program?