Sign in to follow this  
opiaboy

error with PlaySound program in this book...

Recommended Posts

opiaboy    144
I am working out of a book, and my program has an error. This program comes from the CD that accompanies the book. This is the error I am getting: ------ Build started: Project: play_sound, Configuration: Debug Win32 ------ Compiling... dsutil.cpp c:\users\ethan\c++ stuff\sources\chapter09\play_sound\dsutil.cpp(686) : error C2065: 'i' : undeclared identifier Build log was saved at "file://c:\Users\Ethan\C++ Stuff\sources\chapter09\play_sound\Debug\BuildLog.htm" play_sound - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== I am not sure where the error occurs. 'i' appears so many times throughout the code, that I am unsure what to do! I also tryed to compile the next program in the book, and it gives me the same error. I have no clue what is wrong! Any help is greatly appreciated. Thanks!

Share this post


Link to post
Share on other sites
Evil Steve    2017
Sounds like the book was written for VC6, which had a bug in it's for-loop scoping rules, meaning a loop variable could be referenced outside fo the loop.

If you double click on the error message, it'll take you to the line that the error is on. If it's a for(i=0; i<dsomething; ++i) line, just make it for(int i=0; .....etc

Share this post


Link to post
Share on other sites
opiaboy    144
so, this is where the error occurs, and I believe it is what you said.

if(i != m_dwNumBuffers )
return m_apDSBuffer[ i ];
else
return m_apDSBuffer[ rand() % m_dwNumBuffers ];

the i is undeclared here. So, I tried putting 'DWORD' in front of 'i', because that is what 'i' is throughout the rest of the program. But I got this error:

------ Build started: Project: play_sound, Configuration: Debug Win32 ------
Compiling...
dsutil.cpp
c:\users\ethan\c++ stuff\sources\chapter09\play_sound\dsutil.cpp(686) : error C2143: syntax error : missing ',' before '!='
c:\users\ethan\c++ stuff\sources\chapter09\play_sound\dsutil.cpp(688) : error C2181: illegal else without matching if
Build log was saved at "file://c:\Users\Ethan\C++ Stuff\sources\chapter09\play_sound\Debug\BuildLog.htm"
play_sound - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


so, what does this mean? why does it say I didn't match an 'else' to my 'if'?

Share this post


Link to post
Share on other sites
Evil Steve    2017
Ah, ok - that's slightly different. First, double check that that i is from a for() loop. If it is, change the for loop from:
for(int i=0; i<something; ++i)
to:
int i; for(i=0; i<something; ++i)
(Or DWORD, if it's a DWORD rather than an int, or whatever).

Essentially, you're moving the point that i is declared so that it's accessible forther down the function.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this