将一个方法的引用值传递给另一个 class
Passing-by-reference value of a method to another class
我正在尝试对其他 class 中方法结果的值进行操作,我通过引用传递 wingArea
值,我也尝试使用方法 span()
在 clsSpanCalculation
中,但 Xcode 显示:"Member function 'span' not viable: 'this' argument has type 'const clsSpanCalculation', but funtion is not marked 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);
我将不胜感激。
您应该将 span()
标记为 const
,就像您已经使用 get_wingArea()
和 get_aspectRatio()
:
float span() const {
float span;
span = sqrt(aspectRatio*wingArea);
return span;
}
您需要将 span
方法声明为 const
(换句话说,声明它不会修改您提供的实例)。
您需要这样做,因为 rootChord
和 tipChord
采用 const clsSpanCalculation &
参数(换句话说,对常量 clsSpanCalculation 实例的引用)。
float span() const {
...
}
我正在尝试对其他 class 中方法结果的值进行操作,我通过引用传递 wingArea
值,我也尝试使用方法 span()
在 clsSpanCalculation
中,但 Xcode 显示:"Member function 'span' not viable: 'this' argument has type 'const clsSpanCalculation', but funtion is not marked 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);
我将不胜感激。
您应该将 span()
标记为 const
,就像您已经使用 get_wingArea()
和 get_aspectRatio()
:
float span() const {
float span;
span = sqrt(aspectRatio*wingArea);
return span;
}
您需要将 span
方法声明为 const
(换句话说,声明它不会修改您提供的实例)。
您需要这样做,因为 rootChord
和 tipChord
采用 const clsSpanCalculation &
参数(换句话说,对常量 clsSpanCalculation 实例的引用)。
float span() const {
...
}