1

having trouble when trying to compile a template class.

In the .h file

template <typename dataType>
class Node {    
 private:
    dataType nodeData;
    Node<dataType>* nextLink;
    Node<dataType>* previousLink;
 public:    
    Node(const dataType& nodeData);

   // methods

In the .template file

template <typename dataType>
Node<dataType>::dataType Node<dataType>::getData() const {
    return nodeData;
};

The error I get when trying to compile is:

 need ‘typename’ before ‘Node<dataType>::dataType’ because ‘Node<dataType>’ is a dependent scope

 Node<dataType>::dataType Node<dataType>::getData() const {

So then I add typename and it then gives me this error:

error: expected nested-name-specifier before ‘dataType’
       typename dataType getData() const;
                ^
error: expected ‘;’ at end of member declaration
error: declaration of ‘int Node<dataType>::dataType’
error: shadows template parm ‘class dataType’
       template <typename dataType> 
                 ^

What have I done wrong?

NuMs
  • 139
  • 3
  • 15
  • It looks like you put `typename` before `dataType` and not before `Node::dataType` as the compiler indicates. Is this the case? Can you show the source after the code change? – Brian Cain Oct 07 '16 at 04:16
  • 1
    The message says to put `typename` before `‘Node::dataType`. Not before `dataType` – M.M Oct 07 '16 at 04:16
  • @BrianCain don't think it is a duplicate of that – M.M Oct 07 '16 at 04:18
  • It looks like you have put `typename` in the declaration in the class, instead of the implementation. Voting to close as **lacking reproducible example**. – Cheers and hth. - Alf Oct 07 '16 at 04:22
  • That said, the access control with getter member functions is not buying you anything down at this level, for a `Node`. No advantage, but much verbosity and complexity. I'd just remove it, use a simple `struct`. – Cheers and hth. - Alf Oct 07 '16 at 04:23

3 Answers3

2

There is no member called dataType, I assume the return type should be just the template dataType:

template <typename dataType>
dataType Node<dataType>::getData() const {
    return nodeData;
}

The compiler message is misleading in this case as it doesn't find proper definition, it assumes the dataType refers to the template argument.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
2
template <typename DataType>
class Node {
public:
    using dataType = DataType;
private:
    dataType nodeData;
    Node<dataType>* nextLink;
    Node<dataType>* previousLink;
public:    
    Node(const dataType& nodeData);
    dataType getData() const;
};
template <typename DataType>
typename Node<DataType>::dataType Node<DataType>::getData() const {
    return nodeData;
};

specfy typename like this.

http://melpon.org/wandbox/permlink/Agu2s6vw6OLfbbRh

yumetodo
  • 1,147
  • 7
  • 19
0

The presented example code is incomplete, so one would have to guess at the concrete problem.

However, here's how to do that class in a practical way, without problems like the one you're encountering:

template< class Item >
struct Node
{
    Node* next;
    Node* prev;
    Item item;
};

Showing that it's sometimes possible to solve a problem without knowing the exact details.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331