I'm writing a templated audio class (with typename T) for audio manipulation with binary data which is either int16_tor int8_t. Lambda functions are very new to me, so I don't know what is wrong with this function used to calculate the root mean square (RMS). Here is the code:
T calculate_RMS() {
return [&] () {
std::vector<T> squares;
for(int i = 0; i < this->data_vector.size(); ++i) {
squares.push_back(std::pow(this->data_vector[i], 2));
}
return std::sqrt(std::accumulate(squares.begin(), squares.end(), 0) / squares.size());
};
}
The error being thrown is:
audio.h: In instantiation of ‘T YNGMAT005::Audio<T>::calculate_RMS() [with T
= short int]’:
audiodriver.cpp:119:66: required from here
audio.h:178:5: error: cannot convert ‘YNGMAT005::Audio<T>::calculate_RMS()
[with T = short int]::__lambda0’ to ‘short int’ in return
};
^
audio.h: In instantiation of ‘T YNGMAT005::Audio<T>::calculate_RMS() [with T = signed char]’:
audiodriver.cpp:122:65: required from here
audio.h:178:5: error: cannot convert ‘YNGMAT005::Audio<T>::calculate_RMS()
[with T = signed char]::__lambda0’ to ‘signed char’ in return
make: *** [audiodriver.o] Error 1
I was testing this function using int8_tso I think that is why it says T is a short int.
Thanks
().