Help with C++ compile error

M

Mark

Hi,

We have a class that defines an exception:

template <class A, class B, class C, class D > class X : public Y
{
...
class SomeException : public BaseException
{
...
}
};

We need to catch the exception in another class:

template <class Dispatcher> class Something : public YetAnotherClass
{
Dispatcher *fDispatcher;

void HandleMessage(...)
{
try {
...
} catch (Dispatcher::SomeException) // This line has the error
{
...
}
}
....

[some code omitted for brevity]

This code compiles under Sun Workshop 6 update 2 and under Compaq C++
V6.5-004 for OpenVMS Alpha V7.3-1 but not under GNU C++ V3.3.5 and
2.95.4 (on debian Linux).

The error message is "parse error before ';' token" on the 'catch'
line.

Can anyone tell me if this is a compiler bug or is the syntax
incorrect?

TIA, Mark
 
R

Rolf Magnus

Mark said:
Hi,

We have a class that defines an exception:

template <class A, class B, class C, class D > class X : public Y
{
...
class SomeException : public BaseException
{
...
}
};

We need to catch the exception in another class:

template <class Dispatcher> class Something : public YetAnotherClass
{
Dispatcher *fDispatcher;

void HandleMessage(...)
{
try {
...
} catch (Dispatcher::SomeException) // This line has the error

} catch (typename Dispatcher::SomeException)
{
...
}
}
...

[some code omitted for brevity]

This code compiles under Sun Workshop 6 update 2 and under Compaq C++
V6.5-004 for OpenVMS Alpha V7.3-1 but not under GNU C++ V3.3.5 and
2.95.4 (on debian Linux).

The error message is "parse error before ';' token" on the 'catch'
line.

Can anyone tell me if this is a compiler bug or is the syntax
incorrect?

I think the syntax is correct, but not the semanctics. SomeException is a
name dependand on a template argument. In such a situation, you have to
explicitly tell the compiler that it's a type, otherwise it is required to
assume that it's a value.
 
G

Greg

Mark said:
Hi,

We have a class that defines an exception:

template <class A, class B, class C, class D > class X : public Y
{
...
class SomeException : public BaseException
{
...
}
};

We need to catch the exception in another class:

template <class Dispatcher> class Something : public YetAnotherClass
{
Dispatcher *fDispatcher;

void HandleMessage(...)
{
try {
...
} catch (Dispatcher::SomeException) // This line has the error
{
...
}
}
...

[some code omitted for brevity]

This code compiles under Sun Workshop 6 update 2 and under Compaq C++
V6.5-004 for OpenVMS Alpha V7.3-1 but not under GNU C++ V3.3.5 and
2.95.4 (on debian Linux).

The error message is "parse error before ';' token" on the 'catch'
line.

The error is probably a missing "typename" keyword. I would change the
catch line to read:

catch (typename Dispatcher::SomeException)

Greg
 
M

Mark

The error is probably a missing "typename" keyword. I would change the
catch line to read:

catch (typename Dispatcher::SomeException)

Thanks Rolf and Greg, that has solved it :)

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top