Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 664565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:36:49+00:00 2026-05-13T23:36:49+00:00

Here is an exception defined in <stdexcept> : class length_error : public logic_error {

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:36:49+00:00Added an answer on May 13, 2026 at 11:36 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 428k
  • Answers 428k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I do not recommend this, but I managed to work… May 15, 2026 at 1:23 pm
  • Editorial Team
    Editorial Team added an answer This is a flaw in Delphi's implementation of properties. Even… May 15, 2026 at 1:23 pm
  • Editorial Team
    Editorial Team added an answer Since the format of the file is fixed username, password,… May 15, 2026 at 1:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.