Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LD220

macrumors member
Original poster
Jun 3, 2009
35
0
Hi guys. This forum really helped me out last time and I'm hoping you'll be able to again. Like I said in my last thread, I'm new to C++ and using the book 'SAMS Teach Yourself C++ in 10 Minutes" to try to learn on my own. I'm in Lesson 11 and it wants me to throw an exception. I've added the #include <exception> line to the top of my file and using namespace std; was already in there. In main() I use this catch statement (straight from book)

catch (runtime_error RuntimeError)
{
SAMSErrorHandling::HandleRuntimeError(RuntimeError);
}

This is in my Accumulator() function

default:
throw
runtime_error
("Error - Invalid operator");
break;

and this is in my ErrorHandling module
int HandleRuntimeError(runtime_error theRuntimeError)
{
cerr <<
theRuntimeError.what() <<
endl;

return 1;
}

I've uploaded the files as .txt, so it might be easier to help (the header is renamed since before it was .h and it didn't complain about me having 2 files that are the same). I keep getting a ton of errors. Here's what I got when I tried g++ ErrorHandlingModule.cpp PromptModule.cpp main.cpp

ErrorHandlingModule.cpp:27: error: ‘runtime_error’ was not declared in this scope
ErrorHandlingModule.cpp:28: error: expected ‘,’ or ‘;’ before ‘{’ token
main.cpp:2: error: expected constructor, destructor, or type conversion before ‘<’ token
main.cpp: In function ‘float Accumulate(char, float)’:
main.cpp:41: error: ‘runtime_error’ was not declared in this scope
main.cpp: In function ‘int main(int, char**)’:
main.cpp:61: error: expected type-specifier before ‘runtime_error’
main.cpp:61: error: expected `)' before ‘RuntimeError’
main.cpp:61: error: expected `{' before ‘RuntimeError’
main.cpp:61: error: ‘RuntimeError’ was not declared in this scope
main.cpp:61: error: expected `;' before ‘)’ token
main.cpp:65: error: expected primary-expression before ‘catch’
main.cpp:65: error: expected `;' before ‘catch’
main.cpp:70: error: ‘SAMSPrompt’ has not been declared
main.cpp:70: error: ‘UserWantsToContinueYOrN’ was not declared in this scope
main.cpp:73: error: expected `while' at end of input
main.cpp:73: error: expected `(' at end of input
main.cpp:73: error: expected primary-expression at end of input
main.cpp:73: error: expected `)' at end of input
main.cpp:73: error: expected `;' at end of input
main.cpp:73: error: expected `}' at end of input

Does anyone have any idea what's going wrong? Sorry for the really long post.
 

Attachments

  • main.txt
    1.3 KB · Views: 92
  • ErrorHandlingModule.txt
    631 bytes · Views: 138
  • ErrorHandlingModuleh.txt
    279 bytes · Views: 134
Looks like the compiler cannot see a declaration of "runtime_error" where it is needed.

Many people avoid "using" and instead qualify names like

catch (std::runtime_error) ...

It costs you five keystrokes each time, but the good thing is that if you don't know all the "std" identifiers by heart, you still know that runtime_error is supposed to be a std:: thing.

So try std::runtime_error. Check that you include the right header file. In XCode, open a file that doesn't compile, then choose "Preprocess" from the build menu. This will create a window with exactly what is being compiled. Look for runtime_error in there. If it isn't there, maybe you are including the wrong header file. Maybe runtime_error isn't actually defined in <exceptions> but somewhere else.
 
Thanks for your help! I did a little digging and found a file called stdexcept, which includes the runtime_error object. Everything seems to be working fine now.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.