在我们类的中间,目前我们陷入了以下编译错误。不确定是编译器还是我们的代码。任何帮助或指导将不胜感激。
我们的头文件:
//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
我们的 CPP 文件:
//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;
}
最佳答案
您必须在开始函数定义之前关闭类声明结束括号。 //数字列表类的规范文件
#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
关于c++ - 错误 C2535 : 'void NumberList::appendNode(double)' : member function already defined or declared Line 35,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29810913/