简体   繁体   中英

Undeclared identifier, in a node class

I have 2 files: Node.h, Node.cpp,

In Node.h, I create the prototype for the Node class. In the prototype I create a string array 'name'. In the Node.cpp class, I tried to use a function that gives 'name' a value, but i keep getting undeclared identifier even though i identified 'name' in 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));

}

I see three things immediately wrong with the following code.

void setName(char input[256]){
    strncpy(name,input,sizeof(name));
}
  1. You did not provide the class name. This is therefore declaring a static function, and not a class member. You also forgot to do this on your getNode function.

  2. You left out the template statement.

  3. You put a template implementation in a cpp file. Be aware that you cannot compile the cpp file as an object -- it must be included in a header, or you can ditch the file altogether and move your implementation into your header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM