0

I was following a video tutorial for playing music in c++:

https://www.youtube.com/watch?v=tgamhuQnOkM&t=1030s

I am using Dev-C++ and I keep getting an error in the header file that "I'm not supposed to worry about"

The line is:

auto d = std::find(devices.begin(), devices.end(), sOutputDevice);

And the error says:

>92 68  C:\Users\benna\Documents\Starting Over\wave_music\olcNoiseMaker.h   [Error] no matching function for call to 'find(std::vector<std::basic_string<wchar_t> >::iterator, std::vector<std::basic_string<wchar_t> >::iterator, std::wstring&)'

I am a noob here and I really don't get it.

My code is the same, and I've looked at linkers, project settings and other IDEs (VS Code & Eclipse) and nothing changes. (Especially the other IDEs, I can't even get them to run "Hello World".)

I just thought I could learn something through a little experimentation but I am getting nowhere. Is it a problem with my software?

I think it may be a problem with accessing my sound hardware.

I am still learning so any help would be appreciated, thank you!

The header file is here: https://github.com/OneLoneCoder/synth/blob/master/olcNoiseMaker.h

And my main file is this:

using namespace std;

#include "olcNoiseMaker.h"

atomic<double> dFrequencyOutput = 0.0;


double MakeNoise(double dTime)
{
    return 0.5 * sin(440.0 * 2 * 3.14159 * dTime);
    //double dOutput 1.0* sin(dFrequencyOutput * 2.0 * 3.14159 * dTime);
    
    //return dOutput * 0.5;
    
    //if (dOutput > 0.0)
    //  return 0.2;
    //else
    //  return -0.2;
    
}

int main()
{
    wcout << "onelonecoder.com - Synthesizer Part 1" << endl;
    
    // Get all sound hardware
    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 make noise function with sound machine
    sound.SetUserFunction(MakeNoise);
    
    
    while (1)
    {
        
    }
    
    return 0;
}
Asesh
  • 3,186
  • 2
  • 21
  • 31
  • 1
    `using namespace std;` [is a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Doing it in a header file is *double* the badness. Any library which have it in their header files should be avoided like the plague. – Some programmer dude May 09 '22 at 11:45
  • Regarding Dev-C++, and other IDE's, Dev-C++ is a rather bad one really, which comes with an extremely old compiler, it's unmaintained and haven't been updated for a long time. VSCode is well-documented, maintained by one of the major players in the market, and has [good tutorials to enable using C++ in Windows](https://code.visualstudio.com/docs/cpp/config-mingw). – Some programmer dude May 09 '22 at 11:48
  • 3
    It's ok being a "noob here". Everyone was, at some point. C++ is the most complicated and hardest to learn general purpose programming language in use today. In order to learn and understand core C++ fundamentals, one must learn: classes, inheritance, virtual inheritance, polymorphism, overloading, templates, containers, algorithms, concepts, constraints, and many more. One cannot learn any of that from some rambling youtube video or a blog. And only after learning all that it makes sense to branch out in specialized ares, like sound programming. How much of that do you happen to know already? – Sam Varshavchik May 09 '22 at 11:48

1 Answers1

2

This is a bug in the linked header file. It should include <algorithm> which is required for std::find, but doesn't.


I haven't checked the rest of the header file or your posted code for bugs, but I also noticed

while (1)
{
    
}

Infinite loops without IO, atomic, volatile or synchronization operations have undefined behavior in C++. You can't use such a loop to infinitely pause the program or thread.


The header seems to not be tested well in multiple different environments. For example this issue mentions similar problems to get it working under MinGW and has a list of changes that user needed to make.

user17732522
  • 53,019
  • 2
  • 56
  • 105
  • Including worked, and I have been combing through various typos to get the program to work. Now I have found a new dead end. The line `sDevices.push_back(woc.szPname);` gives the error: **[Error] no matching function for call to 'std::vector >::push_back(CHAR [32])'** – Ben Gardner May 11 '22 at 11:56
  • Could there be another include that I am missing? – Ben Gardner May 11 '22 at 12:00
  • 1
    @BenGardner That's a matter of whether the Windows API makes `WAVEOUTCAPS::szPname` a narrow character string or a wide character string. I have no experience with these Windows APIs, but according to https://learn.microsoft.com/en-us/windows/win32/api/mmeapi/ns-mmeapi-waveoutcaps it should be a narrow string, so instead of `wstring` it should be `string`. – user17732522 May 11 '22 at 12:15
  • 1
    @BenGardner The author of the header seems to not have tested the header well in mutliple different environments. You are not the only one that has trouble with it. See [this issue](https://github.com/OneLoneCoder/synth/issues/14), where a user seems to have similar problems as you do and provides a list of changes they needed to do to get it working. – user17732522 May 11 '22 at 12:16