I'm having this problem weird compilation problem:
I try to compile the below sources, but it won't work. It says I haven't implemented the functions declared in linkedlist.h, but they're all in linkedlist.cc. It's like it doesn't read linkedlist.o at all.
Here's the source:
--- Code for linkedlist.h ---
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template <class Data>
class Node {
public:
Node(Data, Node *);
~Node(void);
Node *next;
Data data;
};
template <class Data>
class LinkedList {
public:
LinkedList(void);
~LinkedList(void);
void insert(Data);
Data get(int);
void erase(int);
protected:
Node <Data> *head;
};
#endif /* ! LINKEDLIST_H */
--- Code for linkedlist.cc ---
#include "linkedlist.h"
template <class Data>
Node<Data>::Node(Data ndata, Node *nnext) : next(nnext) {
data = ndata;
}
template <class Data>
Node<Data>::~Node(void) {
}
template <class Data>
LinkedList<Data>::LinkedList(void) {
head = 0;
}
template <class Data>
LinkedList<Data>::~LinkedList(void) {
}
template <class Data>
void LinkedList<Data>::insert(Data data) {
head = new Node<Data>(data, head);
}
template <class Data>
Data LinkedList<Data>::get(int i) {
Data data;
Node <Data> node;
for (int a = 0; a < i; a++) {
if (head == NULL)
return NULL;
head = head->next;
}
data = head->data;
return data;
}
template <class Data>
void LinkedList<Data>::erase(int i) {
bool b = 0;
Node <Data> node;
for (int a = 0; head != NULL; a++) {
if (a == i)
b = 1;
head = head->next;
if (b)
head = head->next;
}
}
--- Code for main.cc ---
#include "linkedlist.h"
int main(void) {
LinkedList <int> hans;
hans.insert(4);
return 0;
}
--- Code for Makefile ---
CFLAGS =
DEST = .
EXTHDRS =
HDRS = linkedlist.h
INSTALL = cp
LD = $(CC)
LDFLAGS =
LIBS =
MAKEFILE = Makefile
OBJS = linkedlist.o \
main.o
PRINT = pr
PROGRAM = a.out
SHELL = /bin/sh
SRCS = linkedlist.cc \
main.cc
SYSHDRS =
all: $(PROGRAM)
$(PROGRAM): $(OBJS) $(LIBS)
@echo "Linking $(PROGRAM) ..."
@$(LD) $(LDFLAGS) $(OBJS) $(LIBS) -o $(PROGRAM)
@echo "done"
clean:; @rm -f $(OBJS) core
clobber:; @rm -f $(OBJS) $(PROGRAM) core tags
depend:; @mkmf -f $(MAKEFILE)
echo:; @echo $(HDRS) $(SRCS)
index:; @ctags -wx $(HDRS) $(SRCS)
install: $(PROGRAM)
@echo Installing $(PROGRAM) in $(DEST)
@-strip $(PROGRAM)
@if [ $(DEST) != . ]; then \
(rm -f $(DEST)/$(PROGRAM); $(INSTALL) $(PROGRAM) $(DEST)); fi
print:; @$(PRINT) $(HDRS) $(SRCS)
tags: $(HDRS) $(SRCS); @ctags $(HDRS) $(SRCS)
###
linkedlist.o: linkedlist.h
main.o: linkedlist.h