Yel*_*aYR 3 c++ arrays linked-list nodes
这是我的班级
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;
};
Run Code Online (Sandbox Code Playgroud)
这是我的追加功能,我无法让它工作 - 我不断收到错误信息.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
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;
Run Code Online (Sandbox Code Playgroud)
关于我做错了什么的任何建议?
所述类型的参数的num中void NumberList::appendNode(double num[])确实是一个指针(= double*),而不是与一定义的元素数目的阵列.
std::array<double,10>在您的结构中使用并作为参数appendNode将是一个很好的解决方案.
这个:
struct ListNode
{
double value[10];
...
Run Code Online (Sandbox Code Playgroud)
变为:
struct ListNode
{
std::array<double,10> value;
...
Run Code Online (Sandbox Code Playgroud)
并且您的函数参数将声明为:
void appendNode(const std::array<double,10>& num);
Run Code Online (Sandbox Code Playgroud)
newNode->value = num; 不需要改变.