1

I am trying to take a list data type I created and make it a template. In doing so I've run into the following obscure problem. I can post all of the code if needed, but this is really the function that is causing the problem.

Note: This code was compiling just fine until I got to this method. I was compiling after writing every few lines as a sanity check, and everything was fine, but then I get to this point and it blows up. If I take the try/catch block out of this method it compiles just fine, so I'm pretty sure the problem is isolated there, not a missing semicolon in a header/etc. as reported from other answers -- though I did of course triple-check to be sure! :)

Here's the code that is causing the problem:

template<class T>
bool UnsortedListType<T>::IsFull()
{
    try { return false; }
    catch(std::bad_alloc exception) { return true; }  // line 35
}

Like I said, I simplified it as much as possible while still triggering the error. Here is the error:

UnsortedListType.cpp||In member function 'bool UnsortedListType<T>::IsFull()':
UnsortedListType.cpp|35|error: expected type-specifier
UnsortedListType.cpp|35|error: expected unqualified-id before 'exception'
UnsortedListType.cpp|35|error: expected ')' before 'exception'
UnsortedListType.cpp|35|error: expected '{' before 'exception'
UnsortedListType.cpp|35|error: 'exception' was not declared in this scope
UnsortedListType.cpp|35|error: expected ';' before ')' token

Everything I can find on this error says the problem is either an extra semicolon or a missing semicolon, either in the header or this file. I can't find an instance of either. And if I remove the try/catch block, it compiles fine.

Plus, if I catch an int, it compiles just fine:

template<class T>
bool UnsortedListType<T>::IsFull()
{
    try { return false; }
    catch(int exception) { return true; }
}

I can also catch(int) and it will compile just fine, but if I try to catch(std::bad_alloc) (i.e. with no "exception" variable name) it throws the same error listed above. Even if I try simply catch(std::exception) it fails to compile.

So now I'm stumped. I'm not an expert at C++ by any stretch, this is for a class, and I'm not sure how to get past this error.

Incidentally, here's the code from the non-generic version, which also compiles just fine, and is verbatim from the textbook I'm using (Dale, if anyone wonders):

bool UnsortedListType::IsFull() const
{
    NodeType* location;
    try
    {
        location = new NodeType;
        delete location;
        return false;
    }
    catch (std::bad_alloc exception)
    {
        return true;
    }
}

I am using CodeBlocks 12.11 IDE on Windows 7 with the built-in GNU compiler.

Any help appreciated, and I'll be happy to post more code if requested, I just didn't want to fill the page up.

Many thanks in advance.

PS I should state, yes I am doing homework, but the homework doesn't call for me to make a template, I am choosing to go that route myself. Not sure if it has any relevance, but this is the first time I've used C++ templates, so just tossing that out there.

Dave
  • 1,057
  • 2
  • 12
  • 18
  • 1
    Catch exceptions by reference. – chris Jul 09 '13 at 01:00
  • You need to `#include` – A. K. Jul 09 '13 at 01:01
  • 5
    Actually, I believe you need to `#include `. And yes, it is recommended that you catch the exception by const-reference: `catch (const std::bad_alloc &exception)`. – jogojapan Jul 09 '13 at 01:02
  • Interesting... #include fixed it, thanks! But why would the line catch(std::bad_alloc exception) compile just fine, taken verbatim from the text, but my version doesn't? And why did it work as-is in my prior code that did not #include but not work now? That is what really has me stumped, that it worked and then suddenly didn't, and I can't find what caused it. :( – Dave Jul 09 '13 at 01:16
  • I should add, I checked all the source files in my original (non-template) project, and none of them had `#include ` in them, yet the same try/catch block worked in them just fine. Both were compiled about a week apart using the same environment, same compiler, etc. – Dave Jul 09 '13 at 01:18
  • Ah [another SO answer](http://stackoverflow.com/questions/2788388/when-is-include-new-library-required-in-c) may have just answered that question for me. I may have been the victim of cascading includes, as my prior project had a few different includes than I have now, so the compiler may have been including `` behind the scenes as it did the compile. That makes sense. Aaaaannnd proven, I just did `#include ` replacing `#include ` and it also compiled, but failed if I removed the include. Thanks for the tip! – Dave Jul 09 '13 at 01:23
  • 1
    @Dave Sorry I saw the updates only now. Yes, `` is included by several other headers, in particular ``, which is in turn included by many parts of the standard library. – jogojapan Jul 09 '13 at 02:19
  • @jogojapan Thanks for the help. I have but one upvote to give, but if you make it an answer I'll select it. Thanks again. – Dave Jul 10 '13 at 03:24

1 Answers1

3

std::bad_alloc is defined in the header <new>, so you need to include that.

Also, it's better to catch exceptions by reference. Catching by value causes a copy, perhaps sliced, of the exception object to be made. Personally I make non-const reference a habit, allowing exception state to be added during handling, but most basic exception types are stateless so there's no practical difference between const & and non-const &.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • What do you mean by a "sliced copy"? – Dave Jul 10 '13 at 03:25
  • @Dave If you initialize an object of a base class (such as `std::exception`) from an instance of a derived class, the copy constructor is used on the base subobject of the original. This may surprise the base class implementation, and in any case it loses information. The effect is called "slicing." – Potatoswatter Jul 10 '13 at 03:35