[英]Undeclared identifier, in a node class
I have 2 files: Node.h, Node.cpp, 我有2个文件:Node.h,Node.cpp,
In Node.h, I create the prototype for the Node class. 在Node.h中,我为Node类创建原型。 In the prototype I create a string array 'name'.
在原型中,我创建一个字符串数组“ 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.cpp类中,我尝试使用一个为'name'赋值的函数,但是即使我在Node.h中识别了'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 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));
}
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. 您还忘记了在
getNode
函数上执行此操作。
You left out the template statement. 您忽略了模板语句。
You put a template implementation in a cpp file. 您将模板实现放在cpp文件中。 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.
请注意,您不能将cpp文件编译为对象-它必须包含在标头中,否则您可以将文件完全抛弃并将实现移入标头中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.