I am trying to implement the queuing mechanism within a circular list. The only problem I am encountering is the following: Basically it tells me that the functions have already been defined and therefore I cannot redefine them.
CLinkedList.cpp:4:1: error: redefinition of 'CircleList<T>::CircleList()'
CircleList<T>::CircleList(){
^~~~~~~~~~~~~
In file included from CLinkedList.h:24:0,
from CLinkedList.cpp:1:
CLinkedList.cpp:4:1: note: 'CircleList<T>::CircleList()' previously declared here
CircleList<T>::CircleList(){
^~~~~~~~~~~~~
CLinkedList.cpp:10:6: error: redefinition of 'bool CircleList<T>::empty() const'
bool CircleList<T>::empty() const{
^~~~~~~~~~~~~
In file included from CLinkedList.h:24:0,
from CLinkedList.cpp:1:
CLinkedList.cpp:10:6: note: 'bool CircleList<T>::empty() const' previously declared here
bool CircleList<T>::empty() const{
^~~~~~~~~~~~~
CLinkedList.cpp:15:10: error: redefinition of 'const T& CircleList<T>::back() const'
const T& CircleList<T>::back() const{
^~~~~~~~~~~~~
In file included from CLinkedList.h:24:0,
from CLinkedList.cpp:1:
CLinkedList.cpp:15:10: note: 'const T& CircleList<T>::back() const' previously declared here
const T& CircleList<T>::back() const{
^~~~~~~~~~~~~
CLinkedList.cpp:20:10: error: redefinition of 'const T& CircleList<T>::front() const'
const T& CircleList<T>::front() const{
^~~~~~~~~~~~~
In file included from CLinkedList.h:24:0,
from CLinkedList.cpp:1:
CLinkedList.cpp:20:10: note: 'const T& CircleList<T>::front() const' previously declared here
const T& CircleList<T>::front() const{
^~~~~~~~~~~~~
CLinkedList.cpp:25:6: error: redefinition of 'void CircleList<T>::advance()'
void CircleList<T>::advance(){
^~~~~~~~~~~~~
In file included from CLinkedList.h:24:0,
from CLinkedList.cpp:1:
CLinkedList.cpp:25:6: note: 'void CircleList<T>::advance()' previously declared here
void CircleList<T>::advance(){
^~~~~~~~~~~~~
CLinkedList.cpp:30:6: error: redefinition of 'void CircleList<T>::add(const T&)'
void CircleList<T>::add(const T& e){
^~~~~~~~~~~~~
In file included from CLinkedList.h:24:0,
from CLinkedList.cpp:1:
CLinkedList.cpp:30:6: note: 'void CircleList<T>::add(const T&)' previously declared here
void CircleList<T>::add(const T& e){
^~~~~~~~~~~~~
CLinkedList.cpp:45:6: error: redefinition of 'void CircleList<T>::remove()'
void CircleList<T>::remove(){
^~~~~~~~~~~~~
In file included from CLinkedList.h:24:0,
from CLinkedList.cpp:1:
CLinkedList.cpp:45:6: note: 'void CircleList<T>::remove()' previously declared here
void CircleList<T>::remove(){
^~~~~~~~~~~~~
CLinkedList.cpp:57:8: error: redefinition of 'std::__cxx11::string CircleList<T>::toString()'
string CircleList<T>::toString(){
^~~~~~~~~~~~~
In file included from CLinkedList.h:24:0,
from CLinkedList.cpp:1:
CLinkedList.cpp:57:8: note: 'std::__cxx11::string CircleList<T>::toString()' previously declared here
string CircleList<T>::toString(){
^~~~~~~~~~~~~
make: *** [CLinkedList.o] Error 1
I enclose the code:
//CNode.h
#ifndef CNODE_H
#define CNODE_H
#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::runtime_error;
using std::ostringstream;
template <typename T> class CircleList;
template <typename T>
class CNode{
private:
T elem;
CNode* next;
friend class CircleList<T>;
};
#endif
//CLinkedList.h
#ifndef CLINKEDLIST_H
#define CLINKEDLIST_H
#include "CNode.h"
template <typename T>
class CircleList{
public:
CircleList();
virtual ~CircleList() = default;
bool empty() const;
const T& front() const;
const T& back() const;
void advance(); //Sposta in avanti il cursore
void add(const T& e);
void remove();
string toString();
private:
CNode<T>* cursor;
};
#include "CLinkedList.cpp"
#endif
//CLinkedList.cpp
#include "CLinkedList.h"
template <typename T>
CircleList<T>::CircleList(){
cursor=nullptr;
}
template <typename T>
bool CircleList<T>::empty() const{
return cursor==nullptr; //Ritorna vero se il cursore non punta a nulla
}
template <typename T>
const T& CircleList<T>::back() const{
return cursor->elem;
}
template <typename T>
const T& CircleList<T>::front() const{
return cursor->nexr->elem;
}
template <typename T>
void CircleList<T>::advance(){
cursor=cursor->next;
}
template <typename T>
void CircleList<T>::add(const T& e){
//Creiamo un nuovo nodo ed inizializziamo
CNode<T>* v = new CNode<T>;
v->elem=e;
//Se la lista è piena dobbiamo creare un nuovo nodo che punta a se stesso
if(cursor==nullptr){
v->next=v;
cursor=v;
}else{ //Altrimenti dobbiamo collegare il nuovo nodo subito dopo il cursore
v->next=cursor->next;
cursor->next=v;
}
}
template <typename T>
void CircleList<T>::remove(){
CNode<T> *old = cursor->next;
//If it is the last node in the list then we must set the cursor to nullptr
if(old==cursor)
cursor=nullptr;
else //If it is not the last node in the list, a 'jump' must be made
cursor->next = old->next;
delete old;
}
template <typename T>
string CircleList<T>::toString(){
ostringstream s;
CNode<T>* u = cursor;
s<<"Cursore -->";
s<<u->elem<<" --> ";
u = u->next;
while(u!=cursor){
s<<u->elem<<" --> ";
u = u->next;
}
s<<"Cursore";
return s.str();
}
//LinkedQueue.h
#ifndef LINKEDQUEUE_H
#define LINKEDQUEUE_H
#include <iostream>
#include <stdexcept>
#include "CLinkedList.h"
using std::cout;
using std::cin;
using std::string;
using std::endl;
using std::runtime_error;
template <typename T>
class LinkedQueue{
public:
LinkedQueue(); //Constructor
int size() const; //Number of elements in the queue
bool empty() const; //Check if the queue is empty
const T& front(); //Returns the element in front position
void enqueue(const T& e); //Adds an element in front position
void dequeue(); //Delete an element in front position
private:
CircleList<T> C; //Circular list of elements
int n; //Number of elements
};
#endif
//LinkedQueue.cpp
template <typename T>
LinkedQueue<T>::LinkedQueue(){
C=nullptr;
n=0;
}
template <typename T>
int LinkedQueue<T>::size() const{
return n;
}
template <typename T>
bool LinkedQueue<T>::empty() const{
return n==0;
}
template <typename T>
const T& LinkedQueue<T>::front(){
if(empty())
throw runtime_error("The queue is empty");
return C.front();
}
template <typename T>
void LinkedQueue<T>::enqueue(const T& e){
C.add(e);
C.advance();
n++;
}
template <typename T>
void LinkedQueue<T>::dequeue(){
if(empty())
throw runtime_error("The queue is empty");
C.remove();
n--;
}
//Main.cpp
#include "LinkedQueue.h"
int main(){
LinkedQueue<int> Q;
Q.enqueue(1);
Q.enqueue(2);
Q.enqueue(3);
cout<<Q.front()<<endl;
Q.dequeue();
cout<<Q.front()<<endl;
Q.dequeue();
cout<<Q.front()<<endl;
Q.dequeue();
return 0;
}
//Makefile
CC=g++
CFLAGS=-std=c++11
DEPS=CLinkedList.h LinkedQueue.h
OBJ= CLinkedList.o LinkedQueue.o main.o
%.o: %.cpp $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
esegui: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS)
clean: $(OBJ)
rm -f *.exe *.o