Wuzaza Wuzaza - 2 months ago 33
C++ Question

Compiler Error C2955

I m studying pointers and templates, and trying to create double linked list with templates. Compiler throws C2955 in 15 line.

#include "stdafx.h"
#include <iostream>

using namespace std;

template<typename T>
struct s_node
{
T value;
s_node *p_next;
s_node *p_prev;
};

template<typename T>
s_node* createNode(s_node *p_parent = NULL) {

s_node *p_newNode = new s_node;

p_newNode->value = 0;
p_newNode->p_next = NULL;

if (p_parent != NULL) {
p_parent->p_next = p_newNode;
}

return p_newNode;
}

Answer

The error is pretty self-explanatory:

error C2955: 's_node': use of class template requires template argument list

s_node is a class template.

Change the code like this:

template<typename T>
s_node<T>* createNode(s_node<T> *p_parent = NULL) {

  s_node<T> *p_newNode = new s_node<T>();

  p_newNode->value = 0;
  p_newNode->p_next = NULL;

  if (p_parent != NULL) {
    p_parent->p_next = p_newNode;
  }

  return p_newNode;
}
Comments