Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 节点类中未声明的标识符_C++_Class_Methods_Nodes_Undeclared Identifier - Fatal编程技术网

C++ 节点类中未声明的标识符

C++ 节点类中未声明的标识符,c++,class,methods,nodes,undeclared-identifier,C++,Class,Methods,Nodes,Undeclared Identifier,我有两个文件:Node.h,Node.cpp 在Node.h中,我为Node类创建了原型。在原型中,我创建了一个字符串数组“name”。在Node.cpp类中,我尝试使用一个给“name”一个值的函数,但我一直得到未声明的标识符,即使我在Node.h中标识了“name” node.h #include "iostream" #include "string.h" #include "stdafx.h" #include "stdio.h" template<class T> cla

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

在Node.h中,我为Node类创建了原型。在原型中,我创建了一个字符串数组“name”。在Node.cpp类中,我尝试使用一个给“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();





};
#包括“iostream”
#包括“string.h”
#包括“stdafx.h”
#包括“stdio.h”
模板
类节点{
字符名[256];
布尔可用;
公众:
//建设者
Node();
节点(const T&item,Node*ptrnext=NULL);
T数据;
//访问下一个节点
Node*nextNode();
//列表修改
void insertAfter(节点*p);
节点*deleteAfter();
Node*getNode(const T&item,Node*nextptr=NULL);
//数据检索
char*getName();
void*setName(char[]);
bool是可用的();
};
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));

}
#包括“Node.h”
//默认构造函数
模板
Node::Node(){
}
//此构造函数设置节点的下一个指针以及该节点中包含的数据
模板
节点::节点(常量和项目,节点*ptrnext){
此->数据=项目;
此->下一步=ptrnext;
}
//此方法在当前节点后插入一个节点
模板
void节点::insertAfter(节点*p){
//将列表的其余部分链接到节点*p
p->next=此->下一步;
//将上一个节点链接到此节点
这个->下一个=p;
}
//此方法从列表中删除当前节点,然后返回它。
模板
Node*Node::deleteAfter(){
节点*temp=next;
如果(下一步!=NULL){
下一步=下一步->下一步;
}
返回温度;
}
模板
Node*getNode(常量T&item,Node*nextptr=NULL){
Node*newnode;//新节点的本地指针
newNode=新节点(项目,nextptr);
if(newNode==NULL){
printf(“错误分配内存”);
出口(1);
}
返回newNode;
}
void setName(字符输入[256]){
strncpy(名称、输入、sizeof(名称));
}

我发现下面的代码有三个问题

void setName(char input[256]){
    strncpy(name,input,sizeof(name));
}
  • 您没有提供类名。因此,这是声明一个静态函数,而不是类成员。您还忘了在
    getNode
    函数中执行此操作

  • 您遗漏了模板语句

  • 将模板实现放入cpp文件中。请注意,您不能将cpp文件编译为对象——它必须包含在头文件中,或者您可以完全放弃该文件,将实现移动到头文件中


  • 您在类定义中声明了一个成员函数
    Node::setName
    ,但从未实际实现过它。相反,您试图实现一个独立的非成员函数
    ::setName
    。该函数对
    节点
    的成员没有任何特定的知识或访问权限。另一个问题是
    strncpy
    在输入太长时不会以null终止字符串,但这与编译错误无关,此外,提供的字符串缓冲区与类中存储的字符串缓冲区大小相同。我有第三个文件,但它与问题无关,所以我没有包含它。但实现在该失败的标题中