开发者

C++ Linked List: Problem with struct inside class

I've been trying to implement a linked list in C++. I found this implementation on the web, where they created a struct for the list's nodes. When trying to add a new node to the list, I get this error:

List.C: In member function `bool Linked::addVertex(Point)':

List.C:23: error: no matching function for call to `Linked::node::node()'

List.H:35: note: candidates are: Linked::node::node(const Linked::node&)

And here's my Code, and thank you very much.. :)

List.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;
 }
}


TAKE HEART! C(++) is a confusing morass of tradition and guesswork when you're learning it. It's a bit like a mouse crawling around inside a Mercedes engine.

Eventually you will feel like you're on the engine design team :)


Your code compiles fine in Visual Studio 2008, but I did use .cpp as the file extension as suggested by other users. Many compilers assume .C files are for straight C, and .cpp file are for C++. You are doing C++

Good Luck!


This type of error would occur if the node struct cannot be constructed automatically. Does your Point class have a constructor with zero arguments? Otherwise, you need to add a constructor to node.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜