c++ - 错误 C2535 : 'void NumberList::appendNode(double)' : member function already defined or declared Line 35

标签 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
    {
        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/

相关文章:

python - 将多维数组与 zip_longest 组合

list - Prolog:将列表中的所有原子小写

c++ - delete[] 触发断点

c++ - 编号的唯一文件

C++:Cast 运算符重载和引用

java - 使用 Item 类的 ArrayList 的销售点应用程序。如何从列表中返回孤立/特定的值?

c - 带参数的宏

c++ - 这是通用 SQL 执行函数的安全实现吗?

c++ - 检查堆栈中是否存在元素

c++ - Linux 执行函数 : what is the arg0 parameter used for?