C++ 错误C2535:&x27;void NumberList::appendNode(双精度)';:已定义或声明的成员函数 ;第35行
在我们的课堂上,目前我们遇到了以下编译错误。不确定是编译器还是我们的代码。任何帮助或指导都将不胜感激 我们的头文件:C++ 错误C2535:&x27;void NumberList::appendNode(双精度)';:已定义或声明的成员函数 ;第35行,c++,list,pointers,C++,List,Pointers,在我们的课堂上,目前我们遇到了以下编译错误。不确定是编译器还是我们的代码。任何帮助或指导都将不胜感激 我们的头文件: //specification file for the numberlist class #ifndef NUMBERLIST_H #define NUMBERLIST_H class NumberList { private: //declares a structure for the list struct ListNode {
//specification file for the numberlist class
#ifndef NUMBERLIST_H
#define NUMBERLIST_H
class NumberList
{
private:
//declares a structure for the list
struct ListNode
{
double value; // value in the node
struct ListNode *next; //to point to the next node
};
ListNode *head; // list head pointer
public:
// construcorr
NumberList()
{
head = nullptr;
}
~NumberList();
//linked list operations
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void dispayList()const;
void NumberList::appendNode(double num)
{
ListNode *newNode;
ListNode *nodePtr;
//allocate a new node and store num there
newNode = new ListNode;
newNode->value = num;
newNode->next = nullptr;
//if there are no nodes in the listmake newNode first node
if (!head)
head = newNode;
else // otherwise insert newNode at end
{
//initialize nodePtr to head of list
nodePtr = head;
//find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// insert newNode as the last node
nodePtr->next = newNode;
}
}
};
#endif
//this program demonstrates a simple append operation on a linked list
#include <iostream>
#include "NumberList.h"
using namespace std;
int main()
{
//define a numberList object
NumberList list;
//append some values
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
system("pause");
return 0;
}
我们的CPP文件:
//specification file for the numberlist class
#ifndef NUMBERLIST_H
#define NUMBERLIST_H
class NumberList
{
private:
//declares a structure for the list
struct ListNode
{
double value; // value in the node
struct ListNode *next; //to point to the next node
};
ListNode *head; // list head pointer
public:
// construcorr
NumberList()
{
head = nullptr;
}
~NumberList();
//linked list operations
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void dispayList()const;
void NumberList::appendNode(double num)
{
ListNode *newNode;
ListNode *nodePtr;
//allocate a new node and store num there
newNode = new ListNode;
newNode->value = num;
newNode->next = nullptr;
//if there are no nodes in the listmake newNode first node
if (!head)
head = newNode;
else // otherwise insert newNode at end
{
//initialize nodePtr to head of list
nodePtr = head;
//find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// insert newNode as the last node
nodePtr->next = newNode;
}
}
};
#endif
//this program demonstrates a simple append operation on a linked list
#include <iostream>
#include "NumberList.h"
using namespace std;
int main()
{
//define a numberList object
NumberList list;
//append some values
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
system("pause");
return 0;
}
//此程序演示了对链表的简单追加操作
#包括
#包括“NumberList.h”
使用名称空间std;
int main()
{
//定义数字列表对象
数字列表;
//附加一些值
附录节点列表(2.5);
附录节点列表(7.9);
附录节点列表(12.6);
系统(“暂停”);
返回0;
}
首先在类声明中声明函数,然后在类声明中定义它。您必须将定义移到实现类的相应.cpp
文件中的外部,即下面的代码应位于实现cpp
文件中:
void NumberList::appendNode(double num)
{
// implementation
}
或者在类中内联定义它
class NumberList
{
// ....
void appendNode(double num) // automatically inline
{
// implement
}
};
第三个选项是在头文件中但在类之外定义它,但是在这种情况下,您必须显式地将其标记为inline
,因为如果在多个cpp
文件中包含头,则会因重复符号而导致链接器错误
inline void NumberList::appendNode(double num) // this can now be in the header file
{
// implementation
}
类中不能同时有声明和定义。在启动函数定义之前,必须关闭类声明结束括号。 //numberlist类的规范文件
#ifndef NUMBERLIST_H
#define NUMBERLIST_H
class NumberList
{
private:
//declares a structure for the list
struct ListNode
{
double value; // value in the node
struct ListNode *next; //to point to the next node
};
ListNode *head; // list head pointer
public:
// construcorr
NumberList()
{
head = nullptr;
}
~NumberList();
//linked list operations
void appendNode(double);
void insertNode(double);
void deleteNode(double);
void dispayList()const;
};
void NumberList::appendNode(double num)
{
ListNode *newNode;
ListNode *nodePtr;
//allocate a new node and store num there
newNode = new ListNode;
newNode->value = num;
newNode->next = nullptr;
//if there are no nodes in the listmake newNode first node
if (!head)
head = newNode;
else // otherwise insert newNode at end
{
//initialize nodePtr to head of list
nodePtr = head;
//find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// insert newNode as the last node
nodePtr->next = newNode;
}
}
#endif
如果在标头内部但在类外部定义成员函数,则应将其标记为
内联
,否则,在不同的编译单元中包含标头将导致多个定义(因此,由于重复符号而导致链接器错误)。最好是将它放在.cpp
中,或者在类中定义它(然后将自动内联)。旁注:用可移植的std::cin.get()替换系统(“暂停”)
代码>。并非所有人都运行Windows。@谢谢,vsoftco可以。在实现的cpp文件中将第一个(将void NumberList::appendNode(double num){}
移动到int main的正上方,返回以下错误,error 1 error LNK2019:未解析的外部符号“public:u thiscall NumberList::~NumberList(void)”(??1NumberList)@@QAE@XZ)函数_main
中引用了带有建议编辑的新代码:@markbratanov您需要创建另一个cpp
文件来实现该类,将其称为NumberList.cpp
。您必须`#包括“NumberList.h”或者,正如我在第二段代码中提到的那样,只定义类内的所有函数。由于您刚刚声明了析构函数,但没有实现它(已定义),因此会出现链接器错误。您必须实现所有函数(至少是您正在使用的函数)。