【问题标题】:How do you write a function prototype in c++ that returns a structure?如何在 C++ 中编写返回结构的函数原型?
【发布时间】:2012-06-13 22:59:21
【问题描述】:

我必须为二次方程创建一个二等分程序。以下是两个单独步骤的说明:

该计划必须满足以下要求:

  1. 二次方程的系数 a、b 和 c 应通过名为 readCoeffs() 的单独函数从键盘读取。该函数应向调用者返回一个读取所有 3 个系数的结构。 t 可以是结构的 3 个字段或具有 3 元素数组的单个字段。

  2. 计算根的函数应以具有 3 个字段的结构形式将结果返回给调用者:root1、root2 和存在(用于确定根是否存在的布尔变量)。

我无法正确编写函数原型和函数标题。当我这样写它们时:

struct calcRoots(double, double, double, double quadfunc(double), bool&);

struct readCoeffs();

int main(){

我得到错误。我认为 Microsoft Visual Studio 期望我创建带有大括号和分号的结构。我很失落。非常感谢任何反馈。

这是我正在使用的整个代码。我不断地修改它。这是我的第一门 C++ 课程。我在挣扎。

/****************************************************************************
//File: bisect.cpp
//Finds the root of a function using the bisection method
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

struct coeffStru{
        double a;
        double b;
        double c;};
struct rootStru{
    double root1;
    double root2;
    bool exists;
               };

//function prototypes
struct calcRoots(double, double, double, double f(double),bool&);
double quadfunc(struct, double);
struct readcoeffs(coeffStru);

int main() {
    double xLeft=-10.0;
    double xRight=10.0; //end points of interval
    double epsilon;       //exists tolerance
    double root;          //root found by bisect
    bool exists;              //exists flag

//Get tolerance
cout<< "Enter tolerance: ";
cin>>epsilon;

struct coeffStru = readcoeffs();

//use bisect calcRoots function to look for root of function f
struct rootStru = calcRoots(xLeft, xRight, epsilon, quadfunc, exists);




//display result
if (!exists){
    cout<< "Root found at "<<root
        <<" \nValue of f(x) at root is: "<<f(root);
        system("PAUSE");
        return 0;
           }
else{
    cout<<"There may be no root in ["<<xLeft<<", "<<xRight<<"]"<<endl;
    system("PAUSE");
    return 0;
    }
}




//Struct return type readcoeffs function
struct readcoeffs(coeffStru){

    cout<<"Enter values for a,b,and c of a quadratic equation."<<endl;
    cin>>a>>b>>c;
    return struct coeffStru;
                            }








//Implements bisection method to find a root of function f
//in interval [xLeft, xRight].
//PRE: xLeft, xRight, and epsilon are defined.
//POST: Returns midpoint of xLeft and xRight if the difference between thses values is <= epsilon. Returns true to exists if there is no root.
struct calcRoots (double xLeft, double xRight, double epsilon, struct coeffStru, double quadfunc(double), bool& exists) //IN: endpoints of interval for possible root, IN: exists tolerance, IN: the function, OUT: exists flag
{
    double xMid;                        //midpoint of interval
    double fLeft;
    double fRight;              //function values at xLeft, xRight,
    double fMid;                        //and xMid


//Compute function values at initial endpoints of interval
    fLeft = quadfunc(xLeft,coeffStru);
    fRight = quadfunc(coeffStru, xRight);


    //Repeat while interval > exists tolerance
    while (fabs ( xLeft - xRight) > epsilon)
    {
        //Compute function value at midpoint
        xMid = (xLeft + xRight)/ 2.0;
        fMid = quadfunc(coeffStru, xMid);

        //Test function value and reset interval if root not found
        if(fMid ==0.0)      //if xMid is the root
            root1 = xMid;    //set root1 to xMid
        else{ 
            xRight = xMid;
            xMid = (xLeft + xRight)/ 2.0;
            fMid = f(coeffStru, xMid);
            }
            if(fLeft * fMid < 0.0)  //root in [xLeft, xMid]





        //Display next interval
        cout<< "New interval is [" <<xLeft
            << ", " <<xRight << "]"<<endl;
    }    //ends 1st while loop


    //If no change of sign in the interval, there is no unique root
    exists = (fLeft * fRight) >0;  //test for same sign - set exists
    if(exists){ 
        return -999.0;      //no 1st root - return to caller
              }


    //Return midpoint of last interval
    root1 = (xLeft + xRight) / 2.0;
//Compute function values at initial endpoints of interval
    fLeft = f(coeffStru, xLeft);
    fRight = f(coeffStru, xRight);



    //Repeat while interval > exists tolerance
    while (fabs ( xLeft - xRight) > epsilon)
    {
        //Compute function value at midpoint
        xMid = (xLeft + xRight)/ 2.0;
        fMid = f(coeffStru, xMid);

        //Test function value and reset interval i root not found
        if(fMid ==0.0)      //xMid is the root
            return xMid;    //return the root
        else if (fLeft * fMid < 0.0)  //root in [xLeft, xMid]
            xRight = xMid;
        else                            //root in [xMid, xRight]
            xLeft = xMid;

        //Display next interval
        cout<< "New interval is [" <<xLeft
            << ", " <<xRight << "]"<<endl;
     }    //ends 2nd while loop


    //If no change of sign in the interval, there is no unique root
    exists = (fLeft * fRight) > 0;  //test for same sign - set exists
    if(exists){ 
        return -999.0;      //no 2nd root - return to caller
              }


    //Return midpoint of last interval
    root2 = (xLeft + xRight) / 2.0;

return struct rootStru;
}   //ends calcRoots method




//Function whose root is being found.
//PRE: x is defined.
//POST: Returns f (x)
double quadfunc(struct coeffStru, double x){
    return a * pow(x, 2.0) - b* pow(x, 1.0) + c;}


************************************************************/

如您所想,我遇到了很多错误。我现在的主要问题似乎是通过引用和值传递变量和结构。

【问题讨论】:

  • 请贴出代码和错误信息!
  • 是的,编译器会期望你知道一些语法。 Perhaps pick up a good book
  • 这听起来像你正在跳进游泳池,你不会游泳。首先学习语法!
  • 您已将返回类型指定为 struct ——但这并没有指定 which struct 是返回类型。您是否已经声明了您将使用的structs?你申报了多少?编译器如何知道每个函数将返回哪一个?为什么您的calcRoots()quadfunc() 作为参数?在什么情况下你会用不同的功能调用calcRoots()
  • @sarnold 有很多学习方法。在您需要从在线论坛寻求帮助之前,还有很多其他资源需要先用尽基础知识……他现在能理解答案吗?

标签: c++ function-prototypes


【解决方案1】:

这是一个两步过程:

(1) 定义保存结果的结构体,可能是这样的:

struct ResultType {
    /* ... fields go here ... */
};

(2)原型化实际功能:

ResultType calcRoots(double a, double b, double c, double function(double), bool& out);

希望这会有所帮助!

【讨论】:

    【解决方案2】:
    struct calcRoots(double, double, double, double quadfunc(double), bool&);
    

    struct 本身不是一个类型。您应该说出要返回的结构的名称。那么,calcRoots 预计会返回什么样的struct

    【讨论】:

      【解决方案3】:

      类似这样的:

      struct Coefficients
      {
          double a, b, c;
      };
      
      struct Roots
      {
          double root1, root2;
          bool exists;
      };
      
      Coefficients readCoeffs() { /* ... */ }
      
      Roots calcRoots(const Coefficients& coefficients) { /* ... */ }
      

      【讨论】:

        【解决方案4】:

        C++ 中的结构类似于 C 语言中的结构。你用同样的方式定义它们。

        struct coeff_t {
           double a;
           double b;
           double c;
        };
        
        struct roots_t {
            bool exist;
            double r1;
            double r2;
        };
        

        现在,通过这些定义,您可以定义函数原型:

        coeff_t readCoeffs();
        
        roots_t calcRoots( coeff_t & coeff );
        

        注意 coeff 参数前面的“&”,这意味着在 C++ 中通过引用传递,这意味着编译器将传递地址,并且知道在函数内部取消引用它,但是您可以在内部将其引用为“coeff”功能。

        【讨论】:

          猜你喜欢
          • 2011-01-10
          • 1970-01-01
          • 1970-01-01
          • 2018-08-06
          • 1970-01-01
          • 2019-08-21
          • 2013-10-12
          • 2014-06-20
          相关资源
          最近更新 更多