将方法的引用值传递给另一个类

时间:2015-07-01 01:13:44

标签: c++ xcode pass-by-reference

我正在尝试使用方法在其他类中的结果的值进行操作,我通过引用传递了span()值,并且我还尝试了clsSpanCalculation中的方法#include <stdio.h> #include <cmath> class clsSpanCalculation{ float wingArea, aspectRatio; public: clsSpanCalculation(){} float get_wingArea(void)const{return wingArea;} void set_wingArea(float Sw){wingArea = Sw;} float get_aspectRatio(void)const{return aspectRatio;} void set_aspectRatio(float AR){aspectRatio = AR;} float span(){ float span; span = sqrt(aspectRatio*wingArea); return span; } }; class clsChordParameters{ float percentRectArea, percertTrapArea, taperRatio; public: float get_percentRectArea(void)const{return percentRectArea;} void set_percentRectArea(float Srect){percentRectArea = Srect;} float get_percentTrapArea(void)const{return percertTrapArea;} void set_percentTrapArea(float Strap){percertTrapArea = Strap;} float get_taperRatio(void)const{return taperRatio;} void set_taperRatio(float lambda){taperRatio = lambda;} float rootChord (const clsSpanCalculation &sC){ float rootChord, lambdaplus; lambdaplus= taperRatio + 1; rootChord = (2*(sC.get_wingArea()*(percentRectArea*(lambdaplus)+(2*percertTrapArea))))/((sC.span()*lambdaplus)/2); return rootChord; } float tipChord (const clsSpanCalculation &sC){ float rootChord, tipChord, lambdaplus; lambdaplus= taperRatio + 1; rootChord = (2*(sC.get_wingArea()*(percentRectArea*(lambdaplus)+(2*percertTrapArea))))/((sC.span()*lambdaplus)/2); tipChord = rootChord*taperRatio; return tipChord; } }; 但是,Xcode显示:“成员函数'span'不可行:'this'参数的类型为'const clsSpanCalculation',但功能未标记为const”

rootChord = (2*(sC.get_wingArea()*(percentRectArea*(lambdaplus)+(2*percertTrapArea))))/((sC.span()*lambdaplus)/2);

以下是Xcode显示消息的代码行:

<form>

我将非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您应该将span()标记为const,就像get_wingArea()get_aspectRatio()一样:

float span() const {
    float span;
    span = sqrt(aspectRatio*wingArea);
    return span;
}

答案 1 :(得分:0)

您需要将span方法声明为const(换句话说,声明它不会修改您提供的实例)。

您需要执行此操作,因为rootChordtipChord采用const clsSpanCalculation &参数(换句话说,是对常量clsSpanCalculation实例的引用)。

float span() const {
   ...
}