0

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;
 }
}
10
  • 3
    Your code is incomplete or you have given the wrong code here. Where is the method addVertex() in the code you have provided? The error you have listed is about a method named addVertex(). Commented Dec 26, 2010 at 14:45
  • @Josh : still string.h, math.h?which compiler are you using? Commented Dec 26, 2010 at 15:01
  • 2
    Why on earth are you using C-style code, C headers and C idioms, and C++ classes? Commented Dec 26, 2010 at 15:03
  • 1
    A comment on style: You should use the extension .cpp or .cc for your implementation file, because some compilers will assume that a file with the extension .c is C only and not C++. Commented Dec 26, 2010 at 15:03
  • What is q = pLinked + index; on SetNode() ?? I think that you think that Linked Lists are Contiguous in Memory. What makes you think that ? And you got many similar stuff on "your" code. Where did you copy that from ? Commented Dec 26, 2010 at 15:06

3 Answers 3

1

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

(This isn't really an answer but I can't figure out how to comment on your original post :) )
You need more reputation to make a comment :)
0

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!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.