错误 C2535:"void NumberList::appendNode(double)":成员函数已定义或声明 第 35 行
Error C2535: 'void NumberList::appendNode(double)' : member function already defined or declared Line 35
在我们类的中间,目前我们陷入了以下编译错误。不确定是编译器还是我们的代码。任何帮助或指导将不胜感激。
我们的头文件:
//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
}
类中不能同时具有声明和定义。
相关文章:
- 如何确保C++函数在定义之前声明(如override关键字)
- 为什么在定义函数之前先声明它
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- Visual Studio中的函数声明和函数定义问题
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 如何在标头中声明(或定义)函数的问题
- 程序顶部的声明与定义(最佳实践)
- 类的前向声明之后的类成员函数定义,在类声明之前
- 静态变量声明和定义
- C++ G++ 编译器 - 错误:隐式声明的定义
- C++:错误重定义和先前声明
- 类模板静态数据成员定义/声明/初始化
- 只有一个定义/声明时标头声明变量的多堆定义错误
- OpenCV - Ptr 语法和类定义/声明 - 混淆?
- 如何为非类型模板类的专用化定义声明之外的方法
- 我如何防止静态类成员变量需要两个定义/声明
- 默认定义声明的详细程度不完整
- C++:非成员函数的定义/声明的位置
- 将比较操作符的重载定义/声明为库中的非成员函数
- C++头重新定义/声明混合