在我的代码的第 138 行,我不断收到 "expected a ';'" 错误(代码 E0065)。我正在使用 Visual Studio 2019 社区版

On line 138 of my code I keep getting an "expected a ';'" error (code E0065). I'm using Visual Studio 2019 Community Edition

我已经包含了到目前为止我编写的所有代码,因为我不确定是否有人想尝试重现该错误。它可能太多了,但我不想遗漏任何东西,以防这是导致错误的原因。

#include

using namespace std;

#include "olcNoiseMaker.h"

上面的行显示了我正在使用的库,这是 link 到 Github 页面的链接,我从中获取它:https://github.com/OneLoneCoder/synth

//Converts frequency (Hz) to angular velocity
double w(double dHertz)
{
 return dHertz * 2.0 * PI;
}

atomic<double> dFrequencyOutput = 0.0;
double dOctaveBaseFrequency = 220.0; //A3
double d12thRootOf2 = pow(2.0, 1.0 / 12.0);
sEnvelopeADSR envelope;

double osc(double dHertz, double dTime, int nType)
{
    switch (nType)
    {
    case 0: // Sine Wave
        return sin(w(dHertz) * dTime);

    case 1: // Square Wave
        return sin(w(dHertz) * dTime) > 0 ? 1.0 : -1.0;

    case 2: // Triangle Wave
        return asin(sin(w(dHertz) * dTime)) * 2.0 / PI;

    case 3: // Saw Wave (analogue / warm / slow)
        {
            double dOutput = 0.0;

            for (double n = 1.0; n < 100.0; n++)
                dOutput += (sin(n * w(dHertz) * dTime)) / n;

           return dOutput * (2.0 / PI);
        }

    case 4: // Saw Wave (optimised / harsh / fast)
        return (2.0 / PI) * (dHertz * PI * fmod(dTime, 1.0 / dHertz) - (PI / 2.0));

    case 5: // Psuedo Random Noise
        return 2.0 * ((double)rand() / (double)RAND_MAX) - 1.0;

    default:
        return 0.0;
    }
}

struct sEnvelopeADSR
{
    double dAttackTime;
    double dDecayTime;
    double dReleaseTime;

    double dSustainAmplitude;
    double dStartAmplitude;

    double dTriggerOnTime;
    double dTriggerOffTime;

    bool bNoteOn;

    sEnvelopeADSR()
    {
        dAttackTime = 0.01;
        dDecayTime = 0.01;
        dStartAmplitude = 1.0;
        dSustainAmplitude = 0.8;
        dReleaseTime = 0.02;
        dTriggerOnTime = 0.0;
        dTriggerOffTime = 0.0;
        bNoteOn = false;
    }

    // Call when key is pressed
    void NoteOn(double dTimeOn)
    {
        dTriggerOnTime = dTimeOn;
        bNoteOn = true;
    }

    // Call when key is released
    void NoteOff(double dTimeOff)
    {
        dTriggerOffTime = dTimeOff;
        bNoteOn = false;
    }

    // Get the correct Amplitude at the requested point in time
    double GetAmplitude(double dTime)
    {
        double dAmplitude = 0.0;
        double dLifeTime = dTime - dTriggerOnTime;

        if (bNoteOn)
        {
            // ADS

            // Attack
            if (dLifeTime <= dAttackTime) {
                dAmplitude = (dLifeTime / dAttackTime) * dStartAmplitude;
            }

            // Decay
            if (dLifeTime > dAttackTime && dLifeTime <= (dAttackTime + dDecayTime)) {
                dAmplitude = ((dLifeTime - dAttackTime) / dDecayTime) * (dSustainAmplitude - dStartAmplitude) + dStartAmplitude;
            }

            // Sustain
            if (dLifeTime > (dAttackTime + dDecayTime))
            {
                dAmplitude = dSustainAmplitude;
            }
            else
            {
                // Release
                dAmplitude = ((dTime - dTriggerOffTime) / dReleaseTime) * (0.0 - dSustainAmplitude) + dSustainAmplitude;
            }

            if (dAmplitude <= 0.0001)
            {
                dAmplitude = 0;
            }

            return dAmplitude;
        }

    }

}

    // Function used by olcNoiseMaker to generate sound
    // Returns amplitude(-1.0 to + 1.0) as a function of time
    
    double MakeNoise(double dTime)

上面一行是我遇到问题的地方。在 Visual Studio 2019 Community Edition 中,我收到一条错误消息,指出在它之前的某处需要一个分号。我不明白这个问题

    {

    }

        int main()
    {

        wcout << "onelonecoder.com - Synthesizer Part 1" << endl;

        // Get all sound behaviors
        vector<wstring> devices = olcNoiseMaker<short>::Enumerate();

        //Display findings
        for (auto d : devices) wcout << "Found Output Device:" << d << endl;

        // Create sound machine!!
        olcNoiseMaker<short> sound(devices[0], 44100, 1, 8, 512);

        // Link noise function with sound machine
        sound.SetUserFunction(MakeNoise);

        while (1)
        {
            //Add a keyboard like a piano
            bool bKeyPressed = false;
            for (int k = 0; k < 15; k++)
            {
                if (GetAsyncKeyState((unsigned char)("ZSXCFVGBNJMK\xbcL\xbe\xbf"[k])) & 0x8000)
                {
                    dFrequencyOutput = dOctaveBaseFrequency * pow(d12thRootOf2, k);
                    envelope.bNoteOn(sound.GetTime());
                    bKeyPressed = true;
                }
            }

            if (!bKeyPressed)
            {
                envelope.bNoteOff(sound.GetTime());
            }
            return 0;
        }
    }

您在 struct osc 定义的最后 } 之后缺少分号。

structclass 定义的结尾需要一个 ;,即使它以 } 结尾。这是您通常只犯一次的错误之一。

这样做的原因是您可以通过将变量名放在 ; 之前来同时声明 struct/class 和创建该类型的变量。这在 C 中使用得更多,在 C++ 中你看不到它。