Compiling error

This is a discussion on Compiling error within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> int main() { try { throw std::exception("test"); } catch(std::exception& e) { std::cerr << e.what() << '\n'; } ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Compiling error

    Code:
    #include <iostream>
    
    int main() {
        try {
            throw std::exception("test");
        } catch(std::exception& e) {
            std::cerr << e.what() << '\n';
        } catch(...) {
            std::cerr << "...\n";
        }
    }
    This compiles under Visual Studio but in g++ it causes an error "error: no matching function for call to `std::exception::exception(const char[5])"

    So why does it work under one compiler but not the other?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,624
    You are probably using a MSVC compiler extension. std::exception does not have a constructor that takes a string of any sort, though std::logic_error and std::runtime_error have constructors that take a std::string by const reference.

    Oh, and you should #include <exception> for std::exception.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21