节点类

时间:2017-04-03 00:20:25

标签: c++ class methods nodes undeclared-identifier

我有2个文件:Node.h,Node.cpp,

在Node.h中,我为Node类创建了原型。在原型中我创建了一个字符串数组' name'。在Node.cpp类中,我尝试使用一个函数,它给出了' name'一个值,但我仍然得到未声明的标识符,即使我确定了' name'在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));

}

1 个答案:

答案 0 :(得分:0)

我在以下代码中看到了三件事。

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

  2. 您遗漏了模板声明。

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