Erik Rasmussen
Erik Rasmussen

Reputation: 331

C++ Beginner - 'error' as a command in code

I am trying to work my way through a c++ textbook to learn some programming, but I've run into an issue with some of the sample code. The goal is to write a simple calculator program, and the text supplies parts of the code as examples. A portion is:

if (!cin) error("no second operand");

The text makes it seem like 'error' will the output the text that comes after it if your input is incorrect, but my compiler just says 'error' hasn't been defined. I'm copying the code from the text word for word, so I'm not sure if I'm missing something with my compiler, or if I've misunderstood how I should be writing the code. Can anyone give some advice?

Upvotes: 3

Views: 125

Answers (3)

jrok
jrok

Reputation: 55395

I assume the book is Stroustrup's Programming Principles and Practices? Check out the pages 149 and 150, it's explained there.

Put this code in a header file and include it in the example program:

#include <stdexcept>

void error(string s)
{
    throw runtime_error(s);
}

void error(string s1, string s2)
{
    throw runtime_error(s1+s2);
}

Upvotes: 1

Yam Marcovic
Yam Marcovic

Reputation: 8141

Maybe they (the authors) didn't mean for you to try to compile that code, but just to understand the example and maybe implement it yourself.

Upvotes: 1

Griwes
Griwes

Reputation: 9029

You must have missed the definition of error() function - it must be somewhere in the book, as it is not C++'s feature.

Upvotes: 5

Related Questions