I have the following function defined inside my linked list class. The declaration in the header file looks like this:
template <typename T>
class RingBuffer
{
...//stuff
static BLink * NewLink (const T&); // allocator
};
BLink is a “link” class within the RingBuffer class. The following implementation code:
template <typename T>
RingBuffer<T>::BLink * RingBuffer<T>::NewLink( const T& t ) // this is line 114
{
// create a new link in linked list
....
....
}
Is giving me this compile error:
./ringbuff.cpp:114: error: expected constructor, destructor, or type conversion before â*â token
I am stumped as to why it it needs an expected constructor, destructor, or type conversion before the return value.
The problem here is that you are referring to a nested dependent type name (i.e. BLink is nested inside RingBuffer which is dependent on a template parameter)
You need to help your compiler a little in this case by stating that
RingBuffer<T>::BLinkis an actual type name. You do this by using thetypenamekeyword.Explanation:
The compiler cannot know if
RingBuffer<T>::BLinkis a type name or a static member until the template parameterTis known. When the compiler parses your function templateTis not known and the rule to solve the ambiguity is to default to “this is not a type name”.Another short example (blatantly copied from Scott Meyers’ Effective C++):
This maybe illustrates the problem a little better as it’s more compact. As already said it’s not clear for the parser whether C::const_iterator is a type name or a static data member as it doesn’t know what
Cis when it parses this part of the code (it may know at a later point in time when the template is actually instantiated). So to ease the compiler implementers’ lives this ambiguity is resolved to “not a type name” and if the programmer wants to use a type name which is nested inside anything that is dependent on a template parameter he/she has to use thetypenamekeyword in front of the name to let the compiler know that it should be treated as a type name.Unfortunately there is an exception to that rule regarding nested dependent type names when using them inside a base class list or the base class identifier in a member initialization list.
Btw: You should set your console client’s charset to UTF-8, so you get
‘*’instead ofâ*â.