簡體   English   中英

方法的按引用值傳遞給另一個類

[英]Passing-by-reference value of a method to another class

我正在嘗試對其他類中的方法的結果值進行操作,我通過引用wingArea值進行傳遞,還嘗試了clsSpanCalculation中的span()方法,但Xcode顯示:“成員函數'span'不是可行:“此”參數的類型為“ const clsSpanCalculation”,但功能未標記為const”

#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顯示消息的代碼行:

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

我將感謝您的幫助。

您應該像使用get_wingArea()get_aspectRatio()一樣將span()標記為const

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

您需要將span方法聲明為const (換句話說,聲明它不會修改您提供的實例)。

您需要執行此操作,因為rootChordtipChord采用的參數是const clsSpanCalculation & (換句話說,是對常量clsSpanCalculation實例的引用)。

float span() const {
   ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM