1

Can someone explain to me how to get this piece of code to compile? What's the syntax error here?

template <typename T>
class Queue
{
  public:
    class Overflow {};
};

template <typename T, typename QUEUE>
class Queue_Adapter : public Queue<T>
{
  public:
    void fun();
};

template<typename T, typename QUEUE>
void
Queue_Adapter<T, QUEUE>::fun()
{
  throw Queue<T>::Overflow();
}

int main()
{
  Queue_Adapter<int, int> tmp;
  tmp.fun();
  return 0;
}

I get the error message below:

test.cpp:19:19: error: 'Overflow' does not refer to a value
  throw Queue<T>::Overflow();
        ~~~~~~~~~~^
test.cpp:25:7: note: in instantiation of member function 'Queue_Adapter<int,
      int>::fun' requested here
  tmp.fun();
      ^
test.cpp:5:11: note: declared here
    class Overflow {};
          ^
1 error generated.

(To get rid of the error message complaining about my question being mostly code...)

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
JT1
  • 443
  • 3
  • 9
  • 3
    Make sure you use `typename` before `Queue`. You really shouldn't be using a nested class for exceptions, especially not one within a template. That's just asking for trouble. – Rufflewind Jan 28 '15 at 22:43
  • I found a C++ class online that had this class structure in the skeleton for part of a larger homework. Could you elaborate on why this is asking for trouble? – JT1 Jan 28 '15 at 22:45
  • Because each instantiation of the template will have a different exception type(!) But yeah, the syntax error is purely due to a dependent type missing the explicit `typename` to help the parser understand that `Overflow` is a type in that context. – Cameron Jan 28 '15 at 22:46
  • The reason you are getting the error is because the compiler thinks `` consists of less-than and greater-than operators! That is, it's parsing your statement as `throw (Queue) < (T) > (::Overflow());` – Rufflewind Jan 28 '15 at 22:49
  • @Rufflewind that's not true - check out the duplicate link to see what's going on. – Drew Dormann Jan 28 '15 at 22:51
  • Yes, if you change it to `throw typename Queue::Overflow();`, it will work. And no, it's not parsing it as less-than and greater-than operators. Rather, since you didn't say that `Overflow` was the name of a type, it assumes that it's the name of a member function that you're trying to call (specifically pointed out in the error message, where it says: "in instantiation of member function..." – Jerry Coffin Jan 28 '15 at 22:53
  • @DrewDormann, JerryCoffin: ah, I see, thanks for the correction! – Rufflewind Jan 28 '15 at 22:55
  • @JerryCoffin: Thanks. Being new to C++ I'm often a bit confused about what the compiler means when it spits out an error. It's even worse if the error is from inside the STL... However, it now makes complete sense. – JT1 Jan 28 '15 at 23:03
  • Yup--completely understandable (just be glad you weren't using C++ 15 to 20 years ago, when error messages were *really* awful). – Jerry Coffin Jan 28 '15 at 23:08
  • I actually did some stuff with C++ in the late 90s (have forgot most of it since then) and remember how a very typical error message was "segmentation fault". Compared to that, it's definitely been improved. ;) – JT1 Jan 29 '15 at 08:34

0 Answers0