gpt4 book ai didi

C++ 链表 : Problem with struct inside class

转载 作者:行者123 更新时间:2023-11-28 08:24:27 24 4
gpt4 key购买 nike

我一直在尝试用 C++ 实现链表。我在网上找到了这个实现,他们在那里为列表的节点创建了一个结构。尝试将新节点添加到列表时,出现此错误:

List.C: 在成员函数`bool Linked::addVertex(Point)'中:

List.C:23: 错误:没有匹配函数来调用 `Linked::node::node()'

List.H:35: 注意:候选者是:Linked::node::node(const Linked::node&)

这是我的代码,非常感谢.. :)

列表.H

#ifndef _AUXILIARY_H_
#define _AUXILIARY_H_
#include <string.h>
#include <math.h>

class Linked
{


public:
// Constructor: initializes a set of nodes
Linked();

// Linked methods
bool addNode(Point p);
bool removeNode(int index);
bool getNode(int index, Point* p) const;
bool setNode(int index, Point p);
int getNodesCount() const;

// Destructor: delete the set of nodes
~Linked();

private:
// Definition of the nodes on the array of nodes
/*typedef struct _Node* pNode;
typedef struct _Node
{
Point pt;
int index;
Node *next;
} Node;
*/
struct node
{
Point pt;
int index;
node *next;
} *pLinked;

// Definition of Bool type
typedef enum {FALSE, TRUE} Bool;
};

#endif // _AUXILIARY_H_

List.C

#include <string.h>
#include "List.H"

Linked::Linked()
{
pLinked=NULL;
}

bool Linked::addNode(Point p)
{
node *q,*t;
int i=0;
q = pLinked;

while (q+i)
{
if ((q->pt.getX() == p.getX()) && (q->pt.getY() == p.getY()))
return FALSE;
q = q->next;
i++;
}

t = new node;
t->pt.setPoint(p);
t->index = getNodesCount();
t->next = q->next;
q->next = t;

return TRUE;
}

bool Linked::removeNode(int index)
{
node *q,*r;
q = pLinked + index;
r = q - 1;
if (q == NULL)
return FALSE;
r->next = q->next;
delete q;
return TRUE;
}

bool Linked::setNode(int index, Point p)
{
node *q;
q = pLinked + index;
if (q == NULL)
return FALSE;
p.setPoint(q->pt);
return TRUE;
}

int Linked::getNodesCount() const
{
node *q;
int count=0;
for( q=pLinked ; q != NULL ; q = q->next )
count++;
return count;
}

Linked::~Linked()
{
node *q;
if( pLinked == NULL )
return;
while( pLinked != NULL )
{
q = pLinked->next;
delete pLinked;
pLinked = q;
}
}

最佳答案

振作精神!当您学习 C(++) 时,它是传统和猜测的困惑泥潭。这有点像老鼠在梅赛德斯引擎里爬来爬去。

最终你会觉得自己是引擎设计团队的一员:)

关于C++ 链表 : Problem with struct inside class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4534163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com