Class not defined. But why?

Hi everybody

I’m just learning how to make classes.
I’ve followed the ofBook OOP chapter but I may have misunderstood something.

I’m trying to transform a function I already have into a class. Its a pretty simple one and I probably didn’t need to do it like so, but if understand how to do this one I’ll be able to do the same with other functions that worth to be transformed into classes.

The original function descends from one in examples/noise. Here it is:

void ofApp::setupNoise(){
    // Setup and allocate resources used in the signed noise.
    nNoiseData = nDelayFrames;
    noiseData = new int[nNoiseData];
    for (int i=0; i < nNoiseData; i++){
        noiseData[i] = 0;
    }
    radialNoiseCursor = 0.0;
}

nDelayFrames is an integer passed from outside. 100 e.g.

and here is the derived class header. I think I don’t need a cpp. Is that right?

#ifndef SETUPNOISE_H
#define SETUPNOISE_H
#include "ofMain.h"

class setupNoise {
    public:
    void setup(){
        nNoiseData = 100;
        radialNoiseCursor = 0.0;
}
    void update(int nNoiseData){
        noiseData = new int[nNoiseData];
        for (int i=0; i < nNoiseData; i++){
            noiseData[i] = 0;
        }
}
    int nNoiseData; // buffer's size
    int *noiseData;
    float radialNoiseCursor;
    setupNoise();
    private: // I couldn't really get what the private functions are for
};

#endif // SETUPNOISE_H

Then I added
#include "setupNoise.h"
and
setupNoise noiseSetup;
in ofApp.h public function
… and in ofApp.cpp setup:
noiseSetup.update(nDelayFrames);

The error I get is (google-translated from German):
error: Undefined reference to `setupNoise :: setupNoise ()

  • this weird:
    file not found /home/gilfuser/main.cpp

Two questions that arise from the error message are: what is this ` in front of setupNoise? And why it is trying to find main.cpp in the home folder and not in the projects scr folder?

Well, this was longer and had more questions than I thought it would. But I think they are all easy for more experienced users, aren’t they?

Thank you all in advance

best regards,
Gil

when you do

setupNoise();

you are declaring a constructor function but not implementing it and that’s what the undefined reference error means. just remove that line or change it with:

setupNoise(){}

also in that class you are declaring a dynamic array of ints in noiseData and then setting it’s size to 100 but since you are never deleting it there’ll be a memory leak, to avoid having to care about deleting memory and make things simpler you can just use a vector instead:

class setupNoise {
    public:
    void setup(){
        radialNoiseCursor = 0.0;
    }
    void update(int nNoiseData){
        // resize to 100 and set all values to 0
        noiseData.assign(nNoiseData,0);
    }
    vector<int> noiseData;
    float radialNoiseCursor;
};

that way also you don’t need the int nNoiseData anymore since you can also simply do: noiseData.size()

1 Like