c++declarationidentifiervst

How do I fix this undeclared identifier? (C++)


I'm writing a VST in C++ currently, and I ran into an issue. I've been surfing the web and StackOverflow for a while now and I'm stuck. Two files are giving me the issue, SynthesizerTwo.h and SynthesizerTwo.cpp.

The error I get is:

'kNumPrograms': undeclared identifier [in SynthesizerTwo.h]

I have a const int called kNumPrograms that is declared like this in SynthesizerTwo.cpp:

#include "SynthesizerTwo.h"

#pragma warning( suppress : 4101 4129 )
#include "IPlug_include_in_plug_src.h"

#include "IControl.h"
#include "resource.h"

const int kNumPrograms = 1; //Original Declaration

enum EParams
{
    kNumParams
};

Then, in SynthesizerTwo.h, it is used in the class SynthesizerTwo like this:

#ifndef __SYNTHESIZERTWO__
#define __SYNTHESIZERTWO__

#include "Oscillator.h"
#include "MIDIReceiver.h"

#pragma warning( suppress : 4101 4129 )
#include "IPlug_include_in_plug_hdr.h"

class SynthesizerTwo : public IPlug
{

public:

  SynthesizerTwo(IPlugInstanceInfo instanceInfo);
  ~SynthesizerTwo();
  //const int kNumPrograms = 5;
  void Reset();
  void OnParamChange(int paramIdx);
  void ProcessDoubleReplacing(double** inputs, double** outputs, int nFrames);

  // to receive MIDI messages:
  void ProcessMidiMsg(IMidiMsg* pMsg);

private:
  double mFrequency;
  void SynthesizerTwo::CreatePresets() {
      
      MakeDefaultPreset((char*)"-", kNumPrograms); //This is what gives me the error
  }
  Oscillator mOscillator;

  MIDIReceiver mMIDIReceiver;
};

#endif

I've tried putting the declaration into the .h file, but then I get an LNK2019 linker error. I've also tried using an extern before the declaration in both the .cpp. and the .h.

I've tried putting the declaration outside the class in the .h, in the public section, and in the private section. I've also tried moving the declaration in the .cpp above the .h #include but I still get errors.

Linker error:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: void __thiscall MIDIReceiver::advance(void)" (?advance@MIDIReceiver@@QAEXXZ) referenced in function "public: virtual void __thiscall SynthesizerTwo::ProcessDoubleReplacing(double * *,double * *,int)" (?ProcessDoubleReplacing@SynthesizerTwo@@UAEXPAPAN0H@Z) SynthesizerTwo-vst2 C:\wdl-ol\IPlugExamples\SynthesizerTwo\SynthesizerTwo.obj   1   

Any help would be much appreciated.


Solution

  • This is what your SynthesizerTwo.cpp file looks like after the pre-processor has included the files, with most cut out:

    class SynthesizerTwo : public IPlug
    {
    ...
      double mFrequency;
      void SynthesizerTwo::CreatePresets() {
          
          MakeDefaultPreset((char*)"-", kNumPrograms); //This is what gives me the error
      }
    ...
    
    const int kNumPrograms = 1; //Original Declaration
    

    As you can see, kNumPrograms is used before it was declared. Declaring it afterwards doesn't help. Besides, if you include "SynthesizerTwo.h" into any other file, there would not be declaration at all.

    Solution: Declare variables before their use.

    But the thing is, I've tried declaring it in the .h and in the .cpp above the .h's #include. It gave me linker errors, as stated in the OP.

    If you define a namespace scope variable in a header, then you should declare it inline.


    #define __SYNTHESIZERTWO__
    

    You've defined an identifier that is reserved to the language implementation. As a consequence, the behaviour of your program is undefined (would be if it compiled in the first place). You should use another header guard.

    (char*)"-"
    

    This seems dangerous. Avoid casting away constness.