Stroustrup PPP: 5.5.1 The caller deals with errors

Hi..

I'm reading about run-time errors in Stroustrup's PPP book, and I don't know why Visual Studio Community 2015 won't when I run the program "terminate the program with a system error message plus the string [I] passed as an argument to error()."

error-message: error C2661: 'error': no overloaded function takes 1 arguments (the error function is defined in the header file "std_lib_facilities.h")

std_lib_facilities.h (header) file: http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Thanks for help!

//

#include "stdafx.h"
#include "../../std_lib_facilities.h"

int area(int length, int width)
{
return length * width;
}

int framed_area(int x, int y)
{
return area(x - 2, y - 2);
}

int main()
{
int x = -1;
int y = 2;
int z = 4;
// . . .
if (x <= 0) error("non-positive x");
if (y <= 0) error("non-positive y");
int area1 = area(x, y);
int area2 = framed_area(1, z);
int area3 = framed_area(y, z);
double ratio = double(area1) / area3;
}
Maybe is defined somewhere else (within stdafx.h?). So try to remove #include "stdafx.h" and see if works then.

using namespace std; is certainly a bad habit especially when it appears within a header file.
First -- check to make sure there's no Windows API headers included through stdafx.h. Win32 has a poor habit of defining macros in all lower-case. (But I don't know if error is one.)
Delete them if they are; your code should compile without anything in your pre-compiled header.

Do you have support for exceptions turned off? Many compilers support a flag (/EHsc in MSVC, -fno-exceptions under G++ or Clang)?

Make sure you've actually copied all of the ../../std-lib-facilities.h correctly.


Last edited on
It isn't required to. Throwing an uncaught exception terminates the program.. it doesn't cause the system to output anything.

If you're debugging, you'll be notified an exception wasn't caught and able to inspect it.
Thank you all!

I proceeded to start a new empty project. I #include[d] the "std_lib_facilities.h" header file, cause the textbook uses it, but now, when I try to run, I get an error message that I could only avoid by adding that "stdafx.h" header file.

The error message is: Unable to start program ...ConsoleApplication1.2.exe. The system cannot find the file specified.

Can I get around this? The idea that "stdafx.h" was preventing a solid call to the error() function defined in "std_lib_facilities.h" is exciting!
closed account (E0p9LyTq)
AfineyoungSc0ut wrote:
Can I get around this? The idea that "stdafx.h" was preventing a solid call to the error() function defined in "std_lib_facilities.h" is exciting!

When you create a new VS 2015 project, create it as an empty project. VS won't require the "precompiled headers" stdafx.h nonsense.

You will have to manually add all your source files, including std_lib_facilities.h. Just copy the file into your project's directory.

I personally create all my new VS projects/solutions as empty projects, unless it is a MFC project. I haven't found a way around that.

Appendix C of Stroustrup's book explains how to set up a project in Visual Studio to work best with his book source code. The steps are for VS 2005, but shouldn't be that hard to figure out for VS 2015.
Last edited on
To go in a bit more on what FurryGuy said, the error isn't in your stdafx.h file. As he said, it's just a precompiled header you get when you don't create an empty project, and it's incredibly annoying because it returns errors for every project source or header file that doesn't include it as either the first non-comment in source files, or after your header guard in header files. Luckily it produces an error that looks nonsensical, then says, "Did you include stdafx.h first?" or something along those lines.

With Stroustrup's custom header, I think the issue you're encountering is that there is custom error handling included in it. I know for certain that he has a predefined error handler included, and I believe it takes the error and a string to print a message to the user. It has been awhile so I don't know for certain, but that's a good candidate for your error message:

error C2661: 'error': no overloaded function takes 1 arguments (the error function is defined in the header file "std_lib_facilities.h")


Could almost be roughly translated as, "The function you've used takes more or fewer arguments than you've passed."

Hope that at least helps a bit.

Also OP, you should probably bookmark or download these files when you get to chapter 11...

https://github.com/Yawzheek/Stroustrup-Graphics

When you go to install FLTK, follow his instructions TO THE LETTER, try (TRY) and use the graphics files he includes on his site here:

http://stroustrup.com/Programming/PPP2code/

And if they don't work, replace them with the codes I've shown you. They're not perfect, I threw them together using help I found all over the internet (after no less than 12 hours of pain trying), but they SHOULD get you up and running, at least enough to get you through the relevant chapters.
Thank you Furryguy and Yawzheek,

I successfully started a project without the "stdafx.h" header file, and will keep those links handy!

Unfortunately, I still get an error, but not the string I attached to the error-handling. Again, the "std_lib_facilities.h" header file should give me this custom error handling.

Unhandled exception at 0x74F3A6F2 in ConsoleApplication1.2.exe: Microsoft C++ exception: std::runtime_error at memory location 0x008FFAC0.
Again, the "std_lib_facilities.h" header file should give me this custom error handling.

There is no "custom error handling" in the header file you linked. Such handling would require a call to std::set_terminate which appears nowhere within.
All std_lib_facilities.h gives you is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Exit : runtime_error {
	Exit(): runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
	throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
	error(s+s2);
}

inline void error(const string& s, int i)
{
	ostringstream os;
	os << s <<": " << i;
	error(os.str());
}

Wouldn't really call them custom.
Excuse my bad terminology. I know you guys are just trying to help.

"Here we have called a function error() which we assume will do something sensible. In fact, in std_lib_facilities.h we supply an error() function that by default terminates the program with a system error message plus the string we passed as an argument to error()." -Stroustrup PPP p.142

Why isn't error() working as it should?

Thank you all again. I'm sorry for long thread!
Last edited on
If you had read my response in your duplicate thread, you'd know you have to catch the thrown exception in a try-catch block:

http://www.cplusplus.com/forum/windows/200332/
Xismn,

In my book, the try-catch block has not been introduced yet.

I explained and apologized for my duplicate thread. I have reported the thread asking for its deletion.
Last edited on
Yeah, you are on 5.5 Run-time errors.
You will get into Exceptions and try-catch blocks in 5.6 Exceptions.

For the record, I've been slowly working through the book for fun. Took a break after reading Chapter 16.
Last edited on
Why isn't error() working as it should?

You said that upon calling the function, you recieve this message:
Unhandled exception at 0x74F3A6F2 in ConsoleApplication1.2.exe: Microsoft C++ exception: std::runtime_error at memory location 0x008FFAC0.

The string doesn't appear, but it's not required to. You can get the string you passed to error() back by calling std::runtime_error::what() on the thrown object.

You'll get to that when you read about the language exception handling mechanism.
Well, the try-catch part worked. I am still confused about the error() function not working, but maybe I'll come to understand it later. For the moment, I can continue with the book!

ThaNKS! THANKS!
Just to clarify, the error() function is supposed to crash your program, unless you make provisions to "catch" or "handle" that error.

Whether or not you see the string you passed into it in the system error message that appears after your program's death -- i.e., as part of the "Unhandled exception at 0x...", is up to the implementation.
Yours doesn't seem to display that info; that's not a problem, and not something you can control.
Last edited on
Topic archived. No new replies allowed.