C ++类 - 如何从另一个成员函数引用成员函数

我对c ++类很新,所以这可能是一个非常明显的问题,但是由于我对术语不熟悉,但我似乎无法获得正确的搜索词。 无论如何,我想要做的是在类中使用公共函数访问同一类中的私有函数。 例如
//.h file:

class foo {

float useful(float, float);

public:

int bar(float);

};

//.cpp file:

int foo::useful(float a, float b){
//does something and returns an int
}

int foo::bar(float a){
//how do I access the 'useful' function in foo?? eg something like
return useful(a, 0.8); //but this doesnt compile
}
    
已邀请:
您的退货类型不符合:
//.h file:

class foo {

float useful(float, float);      // <--- THIS ONE IS FLOAT ....

public:

int bar(float);

};

//.cpp file:

int foo::useful(float a, float b){       // <-- ...THIS ONE IS INT. WHICH ONE?
//does something and returns an int
}

int foo::bar(float a){
//how do I access the 'useful' function in foo?? eg something like
return useful(a, 0.8); //but this doesnt compile
}
编译器会查找完全匹配的函数定义。你得到的编译器错误可能是抱怨它a)找不到
float useful()
,或b)在你谈论
int useful
时不知道你的意思。 确保那些匹配,并在
bar
内调用
useful
应该可以正常工作。     
声明函数
useful
返回
float
,但是将其定义为返回
int
。 对比
float useful(float, float);
VS
int foo::useful(float a, float b){
    //does something and returns an int
}
如果您将声明更改为
int useful(float, float)
并从函数返回一些内容,它将正常工作。     
既然你没有发布编译器给你的错误信息,我会猜测。
useful()
的返回类型与.h和.cpp文件不匹配。如果你使它们匹配(int或者两者都是浮点数),一切都应该按预期工作。     

要回复问题请先登录注册