Compilation error in G++: 'max' was not decoded in this scope

Asked 2 years ago, Updated 2 years ago, 33 views

Thank you for your help.
 I am trying to compile CPP sources using G++ by installing MinGW in Windows 10, but I get an error when I compile the source code below.

#define_USE_MATH_DEFINES
# include <cmath>
# include "beeps.h"

const int sampleRate=44100;
const intamplitude=14000;

int generateBeep(short*buf, const floathz, const int length, const int left, const int right) {
    const int samplesPerCycle=static_cast<int>(sampleRate/hz);
    int totalSamples=static_cast<int>((length/1000.0)/(1.0/sampleRate)));
    totalSamples+=samplesPerCycle-(totalSamples%samplesPerCycle);
    if(!buf) {//just return buffer length
        return totalSamples*4;
    }
    const double lpan=(left/100.0)*amplitude, rpan=(right/100.0)*amplitude;
    const double sinFreeq=(2.0*M_PI)/(sampleRate/hz); // DON'T use samplesPerCycle here
    for(int sampleNum=0; sampleNum<totalSamples;++sampleNum){
        const double sample=min(max(sin(sampleNum%sampleRate)*sinFreq)*2.0, -1.0), 1.0);
        buf[sampleNum*2] = static_cast<short>(sample*lpan);
        buf[sampleNum*2+1] = static_cast<short>(sample*rpan);
    }
    return totalSamples*4;
}

The error details are as follows:

beeps.cpp:18:75:error:'max' was not decoded in this scope
const double sample=min(max(sin(sampleNum%sampleRate)*sinFreq)*2.0, -1.0), 1.0);

beeps.cpp:18:80:error:'min' was not decoded in this scope
const double sample=min(max(sin(sampleNum%sampleRate)*sinFreq)*2.0, -1.0), 1.0);

Are there any packages that need to be installed?
 I would appreciate it if you could let me know if there is any solution.
 Thank you for your cooperation.

c++

2022-09-30 16:30

1 Answers

Standard Library Headers<cmath> do not declare min or max if std::min or std:maxBy the way, using std::clamp in <algorithm>

is

const double sample=min(max(sin(sampleNum%sampleRate)*sinFreq)*2.0, -1.0), 1.0);

is

const double sample=std::clamp(sin(sampleNum%sampleRate)*sinFreq)*2.0,-1.0, 1.0);

can be written to make the code easier to understand.


2022-09-30 16:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.