Topic: Templates problem..


Author: thomas.maeder@transdoc.ch (Thomas Maeder)
Date: 1995/06/20
Raw View
In a previous article you wrote:
>
>template <class keyType, class valueType
>class TDict {
>  struct node{
>    ...
>  }Node;
>
>  typedef struct node *ptrNode;
>  ptrNode list;
>  ptrNode getNode(keyType key, valueType value);
>..
>};
>
>template <class keyType, class valueType>
>ptrNode TDict<keyType, valueType>::getNode(keyType key, valueType value)
>{
>..
>}
>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ptrNode is a type (or rather a synonym of a type) local to the instances (which are classes!)
of the TDict class template. The function definition should look like this:

template <class keyType, class valueType>
TDict<keyType,valueType>::ptrNode TDict<keyType,valueType>::getNode(keyType key, valueType value)
{
  ptrNode result;
.
  return result;
}

Only inside the curly braces you can access ptrNode unqualified.

Thomas






Author: anolas01@solix.fiu.edu (alexander m nolasco)
Date: 1995/06/16
Raw View


Author: anolas01@solix.fiu.edu (alexander m nolasco)
Date: 16 Jun 1995 03:29:08 GMT
Raw View
Here is a newbie's problem..

Let's say the following declarations have been made..

template <class keyType, class valueType
class TDict {
  struct node{
    keyType key;
    valueType value;
    node *next;
    node *prev;
  }Node;

  typedef struct node *ptrNode;
  ptrNode list;

  ptrNode getNode(keyType key, valueType value);

  public:
.
.
.
};

and then ...

template <class keyType, class valueType>
ptrNode TDict<keyType, valueType>::getNode(keyType key, valueType value)
{
.
.
  return (newNode); //Where newNode is a variable of type ptrNode.
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


This does not work, is it Borlandc C++ 3.1 or am I missing something?
I get the message "template must be classes or functions.."

However, if the function above returns a pointer to an integer, it compiles..
Please help..


Thanks in advance.
anolas01@solix.fiu.edu