我正在努力将一些处理从 C++ 驱动程序转移到我工作环境中类中的新方法。我已经开始研究该方法,但是在尝试从新编写的方法中调用类中存在的其他对象相关方法时,我遇到了问题。以下是部分代码:
bool resample_edf(VVectorDouble& sigin_a, VVectorDouble& sig_out_a,
long out_freq_a) {
// display debugging information
//
if (debug_level_d >= Edf::LEVEL_DETAILED) {
fprintf(stdout, "Edf::resample_edf(): starting resample/n");
}
// get the labels in the file
//
char* labels[Edf::MAX_NCHANS];
long num_channels = Edf::get_labels(labels);
long num_samples = (long) Edf::get_duration() * Edf::get_sample_frequency();
...
get_labels(labels)
、get_duration()
和 get_sample_frequency()
方法都是我正在使用的 Edf 类的方法in,但我在尝试编译它时遇到此错误。
edf_01.cc:2240:45: error: cannot call member function ‘long int Edf::get_labels(char**)’ without object
long num_channels = Edf::get_labels(labels);
^
edf_01.cc:2242:47: error: cannot call member function ‘double Edf::get_duration()’ without object
long num_samples = (long) Edf::get_duration() * Edf::get_sample_frequency();
所有方法都是公共(public)方法,但它们使用的一些变量在类中是 protected 。
我不确定如何解决这个问题,但我会继续研究它。谢谢你的帮助。如果需要更多信息,请告诉我。
编辑:我想有一些误解,所以我会提供更多信息。
调用此方法的实用程序中已存在一个 Edf 对象。该实用程序目前看起来像这样:
// local include files
//
#include <Edf.h>
...
int main(int argc, const char** argv) {
...
// create an Edf object
//
Edf edf(Edf::LEVEL_NONE);
// resample the signal
//
if (!edf.resample_edf(sig_in, sig_out, out_freq)) {
fprintf(stdout, " **> nedc_resample_edf: error resampling signal\n");
return((status = -1))
}
...
resample_edf 方法是 edf 对象中的一个方法。现在,在该方法中,我希望能够从该对象调用其他方法,但这样做时出现错误。在此方法中重新实例化 edf 对象对我没有帮助。我尝试了多种方法,但没有任何效果。
我最初没有包括实用程序的运行方式,因为类的编译与实用程序无关。问题来自该方法如何从同一对象调用其他方法。 Edf 类非常大,因此很难为您提供全部内容。
抱歉,如果原始帖子不清楚。
最佳答案
你说你正在将代码移动到类中的一个新方法,但该方法是免费的(它不属于 Edf 类。)使用以下内容使其属于 Edf
类(class):
bool Edf::resample_edf(VVectorDouble& sigin_a, VVectorDouble& sig_out_a,
long out_freq_a)
并确保将此函数原型(prototype)添加到 class Edf {...}
定义中,该定义未在您的问题中显示但假定存在。
有点像
class Edf
{
//...
bool resample_edf(VVectorDouble& sigin_a, VVectorDouble& sig_out_a,
long out_freq_a);
//...
};
您可能会丢失函数定义中的所有 Edf::
。
我还注意到您在 fprintf
中使用了 /n
而不是 \n
关于C++:在另一个方法中调用依赖于对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150453/