James Mchugh
James Mchugh

Reputation: 1014

C++: Calling object dependent methods within another method

I am working on moving some processing from a C++ driver to a new method in a class within the environment in which I work. I have started working on the method, but I am facing problems when attempting to call other object dependent methods that exist in the class from the newly written method. Here is some of the code:

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();

...

The get_labels(labels), get_duration(), and get_sample_frequency() methods are all methods of the Edf class that I am working in, but I get this error when attempting to compile it.

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();

All of the methods are public methods, but some of the variables they use are protected in the class.

I am not exactly sure how to resolve this issue, but I will continue looking into it. Thank you the help. Please let me know if more information is needed.

Edit: I guess there are some misunderstandings so I will provide more information.

An Edf object already exists in the utility that is calling this method. The utility looks something like this at the moment:

// 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))
  }
...

The resample_edf method is a method within the edf object. Now, inside that method, I want to be able to call other methods from the object, but I am getting errors in doing so. Reinstantiating an edf object within this method will not help me here. I have tried multiple ways of doing it, but nothing has worked.

I did not originally include the way the utility is being run because the compiling of the class has nothing to do with the utility. The problem comes from how the method is calling other methods from the same object. The Edf class is very big, so it would be hard to supply you the whole thing.

Sorry if the original post was not clear.

Upvotes: 0

Views: 132

Answers (2)

James Adkison
James Adkison

Reputation: 9602

You haven't shown us the Edf class/struct but the error messages are pretty clear.

error: cannot call member function ... without object

The usage Edf::get_labels(labels); would be correct when calling a static function in the Edf class/struct.

However, for a non-static member function you need an object instance of the class/struct.

For example:

Edf edf; // Of course I don't actually know if this class is default constructible
edf.get_labels(labels);

While something like the above code example will resolve the compilation error it is unlikely to actually solve your problem (i.e., a default constructed object instance probably can't get the labels you're trying to retrieve).

Perhaps an instance of the Edf class needs to be passed to the resample_edf function as a parameter?


From your comment it sounds like resample_edf is a member function of Edf.

bool resample_edf(VVectorDouble& sigin_a, VVectorDouble& sig_out_a,
                  long out_freq_a)
{
    // ...

    // Just call your other member functions directly since `this` is an instance
    // of the `Edf` object
    get_labels(labels);

    // ...
}

Upvotes: 1

T33C
T33C

Reputation: 4429

You said you were moving the code to a new method in the class but the method is free (it doesn't belong to the Edf class.) Use the following to make it belong to the Edf class:

bool Edf::resample_edf(VVectorDouble& sigin_a, VVectorDouble& sig_out_a,
                  long out_freq_a)

And make sure you add this function prototype to the class Edf {...} definition not shown in your question but presumed to exist.

Something like

class Edf
{
//...
bool resample_edf(VVectorDouble& sigin_a, VVectorDouble& sig_out_a,
                      long out_freq_a);
//...
};

You can lose all the Edf:: 's from inside your function definition.

I also noticed that you used /n instead of \n in your fprintf

Upvotes: 1

Related Questions