g++ says declaration and definition don't match, but they do! [SOLVED]

I want to create a chain of FridgeItem* for my Fridge project. I looked up a tutorial on Linked Lists, and have been trying to duplicate the code for the videos project for future reference. Here's the error I'm getting:

1
2
3
4
5
6
LinkedList.cpp:7:6: error: prototype for 'void LinkedList::addToHead(const string&)' does not match any in class 'LinkedList'
 void LinkedList::addToHead(const string& name)
      ^
In file included from LinkedList.cpp:1:0:
LinkedList.h:9:12: error: candidate is: void LinkedList::addToHead(std::string&)
       void addToHead(std::string& name);


Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
michael@caitlyn linkedlists $ cat LinkedList.h LinkedList.cpp
#ifndef _LINKEDLIST_
#define _LINKEDLIST_
#include "Node.h"

class LinkedList
{
   public:
      LinkedList();
      void addToHead(std::string& name);
      
   private:
      Node* head;
      int size;
};

#endif
#include "LinkedList.h"
using namespace std;

LinkedList::LinkedList() : head(0), size(0)
{}

void LinkedList::addToHead(const string& name)
{
   Node* newOne = new Node(name);

   //Is the list currently empty?  Let's check.
   if (head == NULL)
   {
      //It IS empty!
      head = newOne;
   }
   else
   {
      //Nope, it's NOT empty.
      newOne = head;
      head = newOne;
   }
   size++;
}
michael@caitlyn linkedlists $ 


What exactly is g++ complaining about? My prototype matches my definition as far as I can see.
Last edited on
You need to include <string> in LinkedList.cpp
> What exactly is g++ complaining about?

void addToHead(std::string& name); // declaration in line 10, .h

is different from

void LinkedList::addToHead(const string& name) { /* .. // attempted definition (line 24, .cpp) */

The const qualification is an integral part of the type of the parameter.
Adding the const qualifier fixed it. I can't believe I missed that! Thank you for pointing it out to me.
Last edited on
Topic archived. No new replies allowed.