Question

In my class, I have a function declared called PlaySound(std::wstring). This class resides within its own namespace. When I am attempting to call this member function from an instance of this class elsewhere, it is causing linker errors because for some reason it appears to be trying to call PlaySoundW() defined in MMSystem.h. I thought having things in my own namespace was intended to prevent these sort of conflicts?

The linker error:

Error   13  error LNK2019: unresolved external symbol "public: void __thiscall MyNamespace::SoundProcessor::PlaySoundW(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >)" (?PlaySoundW@SoundProcessor@MyNamespace@@QAEXV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z) referenced in function "public: void __thiscall MyNamespace::Engine::Init(struct HWND__ *,long *,long *,int)" (?Init@Engine@MyNamespace@@QAEXPAUHWND__@@PAJ1H@Z)

The best I can understand this, is that it seems to be complaining that I'm using PlaySoundW() in my code, but that I've not defined it in my SoundProcessor class. My function is not called PlaySoundW().

I am calling my function thusly (AND from within the same namespace as the one the function is declared in):

soundProcessor.PlaySound(TEXT("Sounds\\MySound.WAV"));

I'm hoping this is just something obvious I've missed.

Was it helpful?

Solution

My assumption based on the type of error you're getting and the fact that your function is in a separate namespace is that there is the following snippet somewhere (probably in a microsoft include):

#ifdef _UNICODE
#define PlaySound PlaySoundW
#else
#define PlaySound PlaySoundA
#endif // _UNICODE

These types of #define statements are common in microsoft's internal headers because they are designed to handle both unicode and multibyte character encodings.

In order to get around this issue, you might want to put the following before you use your code:

#undef PlaySound
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top