Thread: looser throw specifier for virtual...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    looser throw specifier for virtual...

    Hey, here's the error: "error: looser throw specifier for virtual OutOfMemory::~OutOfMemory
    "
    What does it mean, why does it happen, what do i do to fix? Thanks. (Compiling with GNU compiler)
    Code:
    #include <exception>
    
    using namespace std; 
    
    class OutOfMemory : public std::exception
    {
    	private: 
    		string identification; 
    	
    	public:
    		OutOfMemory(string ident) : identification(ident) {}
    		string what() { return "identification"; }		
    };

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    My compiler continues the error message:
    overriding `virtual std::exception::~exception() throw ()'
    Apparently OutOfMemory should have a destructor which promises not to throw, too.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    Unhappy

    I've never seen a declaration like that before and I don't really know how to promise things to the compiler

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Basically, you define:
    Code:
    ~OutOfMemory() throw() {}
    Incidentally, I note that what() returns a const char*, and is const, and apparently also has throw().

    EDIT:
    Interestingly, I do not recall having to bother with this, but then the only exception classes I have written derive from a subclass of std::exception, e.g., std::logic_error. As such, I could use the constructor that takes a string instead of providing my own string member variable.

    I suggest that you do the same.
    Last edited by laserlight; 02-08-2008 at 09:56 AM.
    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

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Incidentally, I note that what() returns a const char*, and is const, and apparently also has throw().
    You mean that it SHOULD, because right now it calls the std::string constructor with a constant string. This may well cause a memory exception in itself, if the system is very low on memory, since the string constructor will have to allocate memory to store the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You mean that it SHOULD
    No, it does, since we are talking about a method that is supposed to be overriden. At the moment the what() hides the virtual function inherited.

    This may well cause a memory exception in itself, if the system is very low on memory, since the string constructor will have to allocate memory to store the string.
    I think that is related to the current compile error, though I do not quite understand why it affected the destructor instead of the constructor.
    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

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    You mean that it SHOULD, because right now it calls the std::string constructor with a constant string. This may well cause a memory exception in itself, if the system is very low on memory, since the string constructor will have to allocate memory to store the string.

    --
    Mats
    I'm guessing std::terminate() would be called and your program would abort if that happened?

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. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Why does C++ need throw()
    By meili100 in forum C++ Programming
    Replies: 19
    Last Post: 11-10-2007, 12:34 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM