【问题标题】:Visual Studio 2019 JUCE C++ fields "undefined" in cpp filecpp 文件中的 Visual Studio 2019 JUCE C++ 字段“未定义”
【发布时间】:2021-08-10 22:26:30
【问题描述】:

当我尝试在 cpp 文件中初始化或使用我的字段时,当我在头文件中全部定义它们时,我收到一个编译器错误“标识符未定义”。

SynthVoice.H

#pragma once
#include <JuceHeader.h>
#include "SynthSound.h"


class SynthVoice : public juce::SynthesiserVoice
{   
    public:

    bool canPlaySound(juce::SynthesiserSound* sound) override;

    void setPitchBend(int pitchWheelPos);

    float pitchBendCents();

    static double noteHz(int midiNoteNumber, double centsOffset);

    void getOscType(float* selection);

    void getOsc2Type(float* selection);
   
    double setEnvelope();

    void getWillsParams(float* mGain, float* blend, float* pbup, float* pbdn);

    void getFilterParams(float* filterType, float* filterCutoff, float* filterRes);


    void startNote(int midiNoteNumber, float velocity, juce::SynthesiserSound* sound, int currentPitchWheelPosition) override;

    void stopNote(float velocity, bool allowTailOff) override;

    void controllerMoved(int controllerNumber, int newControllerValue) override;

    void prepareToPlay(double sampleRate, int samplesPerBlock, int outputChannels);

    void renderNextBlock(juce::AudioBuffer <float>& outputBuffer, int startSample, int numSamples);


private:
    double level;
    double frequency;
    int theWave, theWave2;
    float masterGain;
    float osc2blend;
    int noteNumber;
    float pitchBend = 0.0f;
    float pitchBendUpSemitones = 2.0f;
    float pitchBendDownSemitones = 2.0f;
    int filterChoice;
    float cutoff;
    float resonance;
    bool isPrepared{ false };
};

SynthVoice.cpp

#pragma once
#include "SynthVoice.h"
class SynthVoice : public juce::SynthesiserVoice
{
public:

    bool SynthVoice::canPlaySound(juce::SynthesiserSound* sound) override
    {
        return dynamic_cast <SynthSound*>(sound) != nullptr;
    }

    void SynthVoice::setPitchBend(int pitchWheelPos)
    {
        if (pitchWheelPos > 8192)
        {
            // shifting up
            pitchBend = float(pitchWheelPos - 8192) / (16383 - 8192);
        }
        else
        {
            // shifting down
            pitchBend = float(8192 - pitchWheelPos) / -8192;    // negative number
        }
    }

    float SynthVoice::pitchBendCents()
    {
        if (pitchBend >= 0.0f)
        {
            // shifting up
            return pitchBend * pitchBendUpSemitones * 100;
        }
        else
        {
            // shifting down
            return pitchBend * pitchBendDownSemitones * 100;
        }
    }

    static double SynthVoice::noteHz(int midiNoteNumber, double centsOffset)
    {
        double hertz = juce::MidiMessage::getMidiNoteInHertz(midiNoteNumber);
        hertz *= std::pow(2.0, centsOffset / 1200);
        return hertz;
    }

    //=======================================================

    void SynthVoice::getOscType(float* selection)
    {
        theWave = *selection;

    }

    void SynthVoice::getOsc2Type(float* selection)
    {

        theWave2 = *selection;
    }
    //=======================================================

  

   
    double SynthVoice::setEnvelope()
    {
        return env1.adsr(setOscType(), env1.trigger);
    }

    //=======================================================

    void SynthVoice::getWillsParams(float* mGain, float* blend, float* pbup, float* pbdn)
    {
        masterGain = *mGain;
        osc2blend = *blend;
        pitchBendUpSemitones = *pbup;
        pitchBendDownSemitones = *pbdn;
    }

    void SynthVoice::getFilterParams(float* filterType, float* filterCutoff, float* filterRes)
    {
        filterChoice = *filterType;
        cutoff = *filterCutoff;
        resonance = *filterRes;
    }

    //=======================================================

    void SynthVoice::startNote(int midiNoteNumber, float velocity, juce::SynthesiserSound* sound, int currentPitchWheelPosition) override
    {
        noteNumber = midiNoteNumber;
        env1.trigger = 1;
        setPitchBend(currentPitchWheelPosition);
        frequency = noteHz(noteNumber, pitchBendCents());
        level = velocity;
    }

    //=======================================================

    void SynthVoice::stopNote(float velocity, bool allowTailOff) override
    {
        env1.trigger = 0;
        allowTailOff = true;

        if (velocity == 0)
            clearCurrentNote();
    }

    //=======================================================

    void SynthVoice::pitchWheelMoved(int newPitchWheelValue) override
    {
        setPitchBend(newPitchWheelValue);
        SynthVoice::frequency = noteHz(noteNumber, pitchBendCents());
    }

    //=======================================================

    void SynthVoice::controllerMoved(int controllerNumber, int newControllerValue) override
    {

    }

    //=======================================================
    void SynthVoice::prepareToPlay(double sampleRate, int samplesPerBlock, int outputChannels)
    {
        isPrepared = true;
    }

    //=======================================================

    void SynthVoice::renderNextBlock(juce::AudioBuffer <float>& outputBuffer, int startSample, int numSamples)
    {
        for (int sample = 0; sample < numSamples; ++sample)
        {
            for (int channel = 0; channel < outputBuffer.getNumChannels(); ++channel)
            {
                outputBuffer.addSample(channel, startSample, setEnvelope() * masterGain);
            }
            ++startSample;
        }
    }


};

在我尝试构建它之前,我在 cpp 文件中的所有私有字段上都得到了红色下划线。我不确定为什么。本来我以为是因为我没有构造函数,但我看的导师不是这样的。

【问题讨论】:

标签: c++ class declaration function-definition


【解决方案1】:

您在标题 SynthVoice.H 中定义了类 SynthVoice

class SynthVoice : public juce::SynthesiserVoice
{
    //...
};

然后用成员函数定义在文件SynthVoice.cpp中重新定义它

class SynthVoice : public juce::SynthesiserVoice
{
    //...
};

如果要定义在头文件中定义的类中声明的成员函数,那么你需要编写例如

bool SynthVoice::canPlaySound(juce::SynthesiserSound* sound)
{
    return dynamic_cast <SynthSound*>(sound) != nullptr;
}

在类定义之外。

这是一个简单的程序,演示如何在声明它们的类之外定义成员函数。

#include <iostream>

class A
{
private:
    int x;
    
public:
    A( int );
    const int & get_x() const ;
    void set_x( int );
};

A::A( int x ) : x( x ) {}

const int & A::get_x() const { return x; }

void A::set_x( int x ) { A::x = x; }

int main() 
{
    A a( 10 );
    
    std::cout << a.get_x() << '\n';
    
    return 0;
}

程序输出是

10

【讨论】:

  • 啊,我知道我用 c++ 编码已经有一分钟了,所以我对语法有点模糊。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多