Heyo,
i am trying to simply read the float value of samples and converting them to a string which i can save for later. I try this using the Decibels::toString method but it just wont work and i dont know why.
Heres the code:
dbVal = Decibels::toString(Decibels::gainToDecibels(sampleValue));
I get the error: operator bool is a private member of juce::String and dont really know why.
Im relatively new to juce development so can anybody help? Am i just missing something?
Platform is OSX
Thanks
What type is the dbVal variable you assign your string result to? I assume that it might be something different than String – so the compiler assumes for some reasons casting the String to bool might be the best fitting choice…
dbVal is from the std::string class
ahh ok i think i got it, i did not know, that juce uses its own String class for this. I m used to C# development so this string -> String stuff seemed to confused me. Thx
Okay, I think you got it. To get a more general understanding of what happened here, classes can define cast-operators that describe what happens when you assign a type of class a to a variable of type b. Sometimes this is a good idea, sometimes it is a cleaner design if you create an explicit member function for that. Example
class Foo
{
public:
Foo (bool b) : value (b) {}
// You might want this special conversion when assigning Foo to an int
operator int() { return value ? 1 : -1; }
// Here you choose an explicit conversion function
String getStringValue() { return value ? "Yes" : "No"; }
private:
bool value;
// For some reasons not designed for the public API
operator float() { return value ? 0.1f : -0.1f; }
}
// Some function that creates a Foo object
Foo createMyFoo() { return Foo (true); };
// Usage
auto f1 = createMyFoo(); // that's how it should be done, auto chooses the best fitting type, in this case the type will be Foo
Foo f2 = createMyFoo(); // does the same job
int f3 = createMyFoo(); // the variable will hold a value of 1
auto f4 = createMyFoo().toString(); // the variable will be of the type String hold the value "Yes"
String f5 = createMyFoo().toString(); // does the same
float f6 = createMyFoo(); // won't work – the cast operator to float is private
float f7 = static_cast<int> (createMyFoo()); // will work, you make use of the public int conversation and then cast this to a float
double f8 = createMyFoo(); // might fail with an "operator float is a private member of Foo" error as float is the next similar type to double
As you can assign a single char to a std::string the compiler probably thought that bool is the next similar type to char and therefore tried to assign this to your std::string. If you are only interfacing between JUCE building blocks, it’s probably the best idea to just use the JUCE String class – I like it a lot more than std::string. If you really need std::string there is the juce::String::toStdString member function in the JUCE String class. And last but not least, if dbVal is a stack variable, you are best off just declaring it as auto, this is a C++ best practice to avoid all kinds of mistakes arising from unwanted casting operations.
1 Like
Thx mr. penguin. Didnt know you could do stuff like this in c++.