views:

120

answers:

4

Here is an exception defined in <stdexcept>:

class length_error : public logic_error 
{
public:
    explicit length_error(const string&  __arg);
};

Here is my exception:

#include <string>
#include <stdexcept>
using namespace std;

class rpn_expression_error : public logic_error
{
public:
    explicit rpn_expression_error(const string& __arg);
};

Why do I get this error when <stdexcept> does not?

Undefined symbols:
  rpn_expression_error::rpn_expression_error(/*string*/ const&), referenced from:
        ...
ld: symbol(s) not found

At @sbi's request, here is a minimal example of my code at the moment:

#include <string>
#include <iostream>
#include <stdexcept>
using namespace std;

class RPN_Calculator {
public:
    class rpn_expression_error : public logic_error {
    public:
        explicit rpn_expression_error(const string& arg) : logic_error(arg) {}
    };

    void Execute() {
        throw rpn_expression_error("Hello");
    }
};

int main() {
    RPN_Calculator calc;

    try {
        calc.Execute();
    } catch (exception e) {
        cout << e.what() << endl;
    }
}

I saved this as rpn.cpp and ran make rpnto produce the error.

The code now builds completely, however, the real program still gives me the original error.

Note/Solution: Although the code above runs just fine, the same exception class in the real code still produces the linker error. To simplify, I just promoted rpn_expression_error to its own global-scope class, and that seems to have fixed the problem.

+3  A: 

There is a problem with the way you are catching your exceptions. Specifically, consider this code:

struct Base
{
    virtual void do() { std::cout << "Base!" << std::endl; }
};

struct Derived : Base
{
    virtual void do() { std::cout << "Derived!" << std::endl; }
};

void foo(Base x)
{
    x.do();
}

int main()
{
    Derived d;
    foo(d); // <--
}

On that marked line, d gets what is called "sliced". That is, in order to satisfy being a Base, everything that isn't part of Base gets sliced off! So the above code will output "Base!".

If we want the intended output, we need to make the parameter not a value:

void foo(Base& x) // polymorphic
{
    x.do();
}

Our above code would then display "Derived!", because it's no longer being sliced. (One could also use a pointer.)

So, take a look at your catch clause:

catch (exception e)

Here, any exceptions you've thrown will be sliced into the base std::exception class, losing any derived information! This is why it's much more common (and possibly "correct") to catch-by-reference:

catch (const exception& e)

You'll now find e.what() returns the non-sliced error message, as intended.

Old

Here's how the entire thing should look (don't use using namespace in a header!):

// rpn_expression_error.h
#include <stdexcept> // for logic_error
#include <string> // for string

class rpn_expression_error : public std::logic_error
{
public:
    explicit rpn_expression_error(const std::string& pMsg);
};

// rpn_expression_error.cpp
#include "rpn_expression_error.h"

rpn_expression_error::rpn_expression_error(const std::string& pMsg) :
std::logic_error(pMsg)
{}

Older

Because those exception classes are declared inside the standard namespace, but yours is not. string is inside the namespace std so they don't need to qualify it, but you do:

#include <string>

// ...
                                    vvv 
explicit rpn_expression_error(const std::string& arg);

Keep in mind I've changed your parameter name. Names that contain a double-underscore are reserved, and you shouldn't use them.

GMan
Although I did forget to `#include <string>;` (doh!), I was `using namespace std;`. I added the `#include`, and nothing. I added the `std::`, and nothing. I took out the double-underscore, and nothing.
Austin Hyde
@Austin: Well, I guess it's time you post a minimal piece of code reproducing the error then, which we can all paste into our editors and try for ourself.
sbi
@Austin: Don't use `using namespace std;` in a header file, you'll get all sorts of headaches from it. (And possibly not ever, but that's more arguable.) I'll edit my answer to show how you should do this thing in its entirety. But like sbi says, we'll need more code to be sure. (And ouch, a down-vote :P)
GMan
Thanks for the tip on `using namespace std`. Did not know that.
Austin Hyde
@Austin: No problem. So maybe I'm being silly, but is the problem fixed? The code you've given above (the full code) shouldn't have any changes for making it global scope. (That is, it should work fine as is, with the changes I just added to my answer.)
GMan
@GMan: Yeah, everything works now. I've removed the `using`, promoted `rpn_expression_error` up from being a nested class (not a problem, but simplifies life), added a definition in addition to a declaration for the constructor, and am now catching by reference (didn't know that one either). Thanks for all the help!
Austin Hyde
@Austin: Anytime. :]
GMan
+1  A: 

It looks like you've declared a constructor but not provided a definition for one.

Adrian McCarthy
It seems that is it...
coelhudo
+1  A: 

It says the function is undefined because you forgot to define it. Try this:

#include <string>
#include <stdexcept>
using namespace std;

class rpn_expression_error : public logic_error
{
public:
    explicit rpn_expression_error(const string& arg)
      : logic_error( arg ) { } // definition
};

As others have suggested, using namespace is poor practice in a header file, so if this is a header,

#include <string>
#include <stdexcept>

class rpn_expression_error : public std::logic_error
{
public:
    explicit rpn_expression_error(const std::string& arg)
      : logic_error( arg ) { } // definition
};

Also, unless your RPN expressions are all hardcoded, errors in them would be runtime_errors, not logic_errors.

Potatoswatter
Thanks for the note about the runtime vs. logic errors. The way I read the documentation, it sounded like `logic_error` represented something regarding human error, and `runtime_error` represented something regarding technical errors.
Austin Hyde
A: 

If that's your code, then the problem is (as I suggested and Adrian also said) that you have declared a constructor for your rpn_expression_error class, but you have not defined it. Try adding this to your code (underneath your class declaration):

rpn_expression_error::rpn_expression_error(const string& arg)
  : logic_error(arg)
{
}
Tyler McHenry