[英]Append won't work in Linked List of Arrays in C++
This is my class 这是我的班级
class NumberList
{
private:
// Declare a structure for the list
struct ListNode
{
double value[10]; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
// Constructor
NumberList()
{ head = nullptr; }
// Destructor
~NumberList();
// Linked list operations
void appendNode(double []);
void insertNode(double []);
void deleteNode(double []);
void displayList() const;
};
This is my append function and I can't get it to work -- i keep getting an error message. 这是我的追加功能,我无法让它工作 - 我不断收到错误信息。
void NumberList::appendNode(double num[])
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
newNode->next = nullptr;
// If there are no nodes in the list
// make newNode the 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;
}
}
Error message: 错误信息:
prog.cpp: In member function ‘void NumberList::appendNode(double*)’: prog.cpp:40:19: error: incompatible types in assignment of ‘double*’ to ‘double [10]’ newNode->value = num;
Any suggestions about what i'm doing wrong? 关于我做错什么的任何建议?
The type of the parameter num
in void NumberList::appendNode(double num[])
is really a pointer (= double*
), rather than an array with a defined number of elements. void NumberList::appendNode(double num[])
中的参数num
的类型实际上是一个指针(= double*
),而不是具有已定义数量的元素的数组。
Using std::array<double,10>
in your structure and as the parameter to appendNode
would be a good solution. 在结构中使用
std::array<double,10>
并作为appendNode
的参数将是一个很好的解决方案。
This: 这个:
struct ListNode
{
double value[10];
...
Becomes: 变为:
struct ListNode
{
std::array<double,10> value;
...
And your function parameters would be declared as: 并且您的函数参数将声明为:
void appendNode(const std::array<double,10>& num);
newNode->value = num;
requires no change. 不需要改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.