未声明的标识符,在节点 class 中
Undeclared identifier, in a node class
我有 2 个文件:Node.h、Node.cpp、
在 Node.h 中,我为节点 class 创建了原型。在原型中,我创建了一个字符串数组 'name'。在 Node.cpp class 中,我尝试使用一个为 'name' 赋值的函数,但即使我在 Node.h 中标识了 'name',我仍然收到未声明的标识符
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));
}
我发现以下代码有三处错误。
void setName(char input[256]){
strncpy(name,input,sizeof(name));
}
您没有提供 class 姓名。因此,这是在声明一个静态函数,而不是 class 成员。您还忘记了在 getNode
函数上执行此操作。
您遗漏了模板语句。
您将模板实现放在 cpp 文件中。请注意,您不能将 cpp 文件编译为 object——它必须包含在 header 中,或者您可以完全放弃该文件并将您的实现移至 header。
我有 2 个文件:Node.h、Node.cpp、
在 Node.h 中,我为节点 class 创建了原型。在原型中,我创建了一个字符串数组 'name'。在 Node.cpp class 中,我尝试使用一个为 'name' 赋值的函数,但即使我在 Node.h 中标识了 'name',我仍然收到未声明的标识符
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));
}
我发现以下代码有三处错误。
void setName(char input[256]){
strncpy(name,input,sizeof(name));
}
您没有提供 class 姓名。因此,这是在声明一个静态函数,而不是 class 成员。您还忘记了在
getNode
函数上执行此操作。您遗漏了模板语句。
您将模板实现放在 cpp 文件中。请注意,您不能将 cpp 文件编译为 object——它必须包含在 header 中,或者您可以完全放弃该文件并将您的实现移至 header。