2017-04-03 30 views
0

我有2个文件:Node.h,Node.cpp,未声明的标识符,它的节点类

在Node.h,创建原型为Node类。在原型中,我创建了一个字符串数组'name'。在Node.cpp类,我试图用一个函数,提供“姓名”的值,但我不断收到未声明的标识符,即使我在Node.h

确定“名”

node.h

#include "iostream" 
#include "string.h" 
#include "stdafx.h" 
#include "stdio.h" 

template<class T> 
class Node{ 

     char name[256]; 
     bool useable; 


    public: 
     //Constructors 
     Node(); 
     Node(const T& item, Node<T>* ptrnext = NULL); 

     T data; 
     //Access to next Node 
     Node<T>* nextNode(); 
     //List modification 
     void insertAfter(Node<T>* p); 
     Node<T>* deleteAfter(); 
     Node<T>* getNode(const T& item, Node<T>* nextptr = NULL); 
     //Data Retrieval 
     char *getName(); 
     void *setName(char[]); 
     bool isUsable(); 





}; 

node.cpp

#include "Node.h" 

//Default Constructor 
template<class T> 
Node<T>::Node(){ 

} 

//This constructor sets the next pointer of a node and the data contained in that node 
template<class T> 
Node<T>::Node(const T& item,Node<T>* ptrnext){ 
    this->data = item; 
    this->next = ptrnext; 
} 

//This method inserts a node after the current node 
template<class T> 
void Node<T>::insertAfter(Node<T> *p){ 
    //Links the rest of list to the Node<T>* p 
    p->next = this->next; 

    //Links the previous node to this one 
    this-> next = p; 
} 

//This method deletes the current node from the list then returns it. 
template<class T> 
Node<T> * Node<T>::deleteAfter(){ 

    Node<T>* temp = next; 

    if(next !=NULL){ 
     next = next->next; 
    } 

    return temp; 
} 

template<class T> 
Node<T> * getNode(const T& item, Node<T>* nextptr = NULL){ 
    Node<T>* newnode; //Local pointer for new node 
    newNode = new Node<T>(item,nextptr); 
    if (newNode == NULL){ 
     printf("Error Allocating Memory"); 
     exit(1); 
    } 
    return newNode; 

} 

void setName(char input[256]){ 
    strncpy(name,input,sizeof(name)); 

} 
+0

你已经在类定义中声明了一个成员函数'Node :: setName',但是你从来没有真正实现它。相反,你试图实现一个独立的非成员函数':: setName'。该函数没有任何关于“Node”成员的特定知识或访问权限。 –

回答

0

我看到三件事情,立即错用下面的代码。

void setName(char input[256]){ 
    strncpy(name,input,sizeof(name)); 
} 
  1. 你没有提供的类名。因此这是声明一个静态函数,而不是一个类成员。您也忘记在您的getNode功能上执行此操作。

  2. 您遗漏了模板语句。

  3. 您将模板实现放在cpp文件中。请注意,您无法将cpp文件编译为对象 - 它必须包含在标题中,或者可以将文件完全丢弃并将您的实现移动到标题中。

+0

另一个问题是,如果输入太长,'strncpy'不会终止字符串 –

+0

True,但这与编译错误无关,除了提供的字符串缓冲区与存储在类中的字符串缓冲区大小相同。 – paddy

+0

我有第三个文件,但它与问题无关,所以我没有包含它。 但是,实现是在一个头失败 – Chaospyke