错误 C2535:"void NumberList::appendNode(double)":成员函数已定义或声明 第 35 行

Error C2535: 'void NumberList::appendNode(double)' : member function already defined or declared Line 35

本文关键字:定义 声明 函数 NumberList void C2535 appendNode 错误 double 成员      更新时间:2023-10-16

在我们类的中间,目前我们陷入了以下编译错误。不确定是编译器还是我们的代码。任何帮助或指导将不胜感激。

我们的头文件:

//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

您首先在类声明中声明了函数,然后在类声明中定义了它。你必须将定义移到外面,在实现类的相应.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
}

类中不能同时具有声明定义。