0

I'm trying to create a linked list class in Eclipse but I can't get it to compile properly.

Here is my .cc file (code snipet)

#include <iostream>
#include "list.h"

using namespace std;

template <class T>
bool List<T>::isEmpty()
{
    return (firstNode == NULL);
}

and here is my list.h file (code snipet)

#ifndef __LIST_H__
#define __LIST_H__

template <typename T>
class List {

public:

    bool isEmpty();

 private:
    struct node {
    node   *following;
    node   *previous;
    T      *contents;
    };

    node   *firstNode;
};

#include "list.cc"

#endif /* __LIST_H__ */

I try "Building All" in eclipse but I get the following error:

make all 
Building file: ../list.cc
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"list.d" -MT"list.d" -o "list.o"     "../list.cc"
../list.cc:13: error: redefinition of 'bool List<T>::isEmpty()'
../list.cc:13: error: 'bool List<T>::isEmpty()' previously declared here
make: *** [list.o] Error 1

Help please...thanks. I'll be happy to provide any clarifications needed

EDIT: I was given the .h file so I know that it is correct. I also know that I am supposed to have a .cc file called list.cc (it is included at the end of the .h file)

Nosrettap
  • 10,940
  • 23
  • 85
  • 140
  • possible duplicate of [Why should the implementation and the declaration of a template class be in the same header file?](http://stackoverflow.com/questions/3749099/why-should-the-implementation-and-the-declaration-of-a-template-class-be-in-the) – Bo Persson Jan 29 '12 at 17:58
  • Try making `isEmpty` inline or static. – Some programmer dude Jan 29 '12 at 18:03
  • @Nosrettap, what are the filenames? Is it `list.cc` or `dlist.cc`? You need to be very clear in your question what the actual filenames are. `list.h` or `dlist.h`? – Aaron McDaid Jan 29 '12 at 18:07
  • @Aaron McDaid, Sorry about that. Everything should be list.h but I don't think that's the problem. I've made the appropriate changes – Nosrettap Jan 29 '12 at 18:16

3 Answers3

3

You need to change the extension of the file with the implementation.

The compiler will process this file for compilation and will process it twice, since you're including it in the header.

Your file looks like this:

#include <iostream>
#include "list.h"

using namespace std;

template <class T>
bool List<T>::isEmpty()
{
    return (firstNode == NULL);
}

which will become

#include <iostream>
#ifndef __DLIST_H__
#define __DLIST_H__

template <typename T>
class List {

public:

    bool isEmpty();

 private:
    struct node {
    node   *following;
    node   *previous;
    T      *contents;
    };

    node   *firstNode;
};

#include "dlist.cc"

#endif /* __DLIST_H__ */

using namespace std;

template <class T>
bool List<T>::isEmpty()
{
    return (firstNode == NULL);
}

which will in turn become

#include <iostream>
#ifndef __DLIST_H__
#define __DLIST_H__

template <typename T>
class List {

public:

    bool isEmpty();

 private:
    struct node {
    node   *following;
    node   *previous;
    T      *contents;
    };

    node   *firstNode;
};

template <class T>
bool List<T>::isEmpty()
{
    return (firstNode == NULL);
}

#endif /* __DLIST_H__ */

using namespace std;

template <class T>
bool List<T>::isEmpty()
{
    return (firstNode == NULL);
}

So the function isEmpty() is defined twice.

Rename the file to dlist.impl.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • I figured it out. I needed to tell eclipse to exclude list.cc from the build because it is implicitly included in the .h file. I'm still giving you best answer though – Nosrettap Jan 29 '12 at 18:45
  • @Nosrettap that was actually the reason I pointed out. Also, you should probably also rename the file. The common extension is `.impl` and you should stick to that. – Luchian Grigore Jan 29 '12 at 18:57
  • @Nosrettap cc is equivalent to cpp as hpp is equivalent to h. – Luchian Grigore Jan 29 '12 at 22:23
0

Try putting the definition for List<T>::isEmpty() in the same file as the class is declared.

josephthomas
  • 3,256
  • 15
  • 20
0

Given the unusual form of the header you've been supplied with, to test it you will need another source file. To start with the new source file (say test.cpp) can just #include "list.h", which will check for any syntax errors but will not yet instantiate your List template.

(Just compile test.cpp, not list.cc, since list.cc is indirectly included by test.cpp)

James Hopkin
  • 13,797
  • 1
  • 42
  • 71