Nosrettap
Nosrettap

Reputation: 11340

How to compile a templated class (C++) in Eclipse?

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)

Upvotes: 0

Views: 1343

Answers (3)

James Hopkin
James Hopkin

Reputation: 13973

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)

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

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.

Upvotes: 3

josephthomas
josephthomas

Reputation: 3276

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

Upvotes: 0

Related Questions