I am trying to write a program that will take a list of integers from a file and write them to another text file. I've been banging my head at this for days trying to get it to compile as it is riddled with linker and compiler errors. Any help is appreciated.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
usingnamespace std;
template <class T>
class linkedList
{
public:
//default constructor
//Description: sets the head to null
//Parameters: none
//Return: none
linkedList() {head = NULL; current = NULL; previous = NULL;}
//The Big 3
//copy constructor
//Description: Copy constructor does a deep copy
//Parameters: list to copy from
//Return: none
linkedList(const linkedList &list);
//operator=
//Description: Assignment operator does a deep copy
//Parameter: list to copy form
//Return: the instance of the lit
linkedList& operator=(const linkedList &list);
//destructor
//Description: Destructor deletes all the nodes in the list
//Paramters: none
//Return : none
~linkedList();
//append
//Description: inserts value at the end of the linked list
//Parameters: Value to be appended
//Return: None
void append(const T &value);
//The following routines are for traversing the list in an application
//retrieve
//Description: retrieves the value at the current position
// value is returned through the parameter
//Paramterss: variable for value to retrieve
//Return: True if the position is valid and false otherwise
bool retrieve(T &value) const;
//begin
//Description: sets the current position to the beginning of the list
// It also sets the previous to NULL
//Paramters: none
//Return: none
void begin();
//operator ++
// Description: as long as the current position is not NULL, it
// moves the current position (and previous) to next item
// Two prototypes are given so the operator can be used both pre\
\
//fix and postfix.
//Parameters: none (int i as a dummy)
//Return: the list
linkedList operator++();
linkedList operator++(int i); //dummy param
private:
class node
{
public:
node() {next = NULL;}
node(const T &value, node *p= NULL) {data = value; next = p;}
T data;
node *next;
};
//attributes
node *head; //points to the beginning of the list
node *current; //keeps track of where the traversal is
node *previous; //keeps track of what's before current
};
//ostream operator for printing the list
template <class T>
ostream &operator<<(ostream &output, linkedList<T> list);
#include "dataStrucImp.cpp"
#endif
#include <iostream>
usingnamespace std;
//default Constructor
//Sets head to NULL
//No Parameters
//No returns
template <class T>
linkedList<T>::linkedList(const linkedList &list)
{
head=NULL;
current=NULL;
previous=NULL;
}
//Copy Constructor
//Performs deep copy
//Parameters are the lists to copy from
//no return
template <class T>
linkedList<T>::linkedList(const linkedList &list)
{
node *p = list.head;
while(p != NULL)
{
insert(p -> data);
p = p -> next;
}
}
//operator=
//Assignment operator performs deep copy
//returns the instance of the list
//contains the list to copy from
template <class T>
linkedList<T> &linkedList<T>::operator=(const linkedList<T> &list)
{
node *p = list.head;
if(this!=&list)
clear();
p=list.head;
while (p!=NULL)
{
insert(p->data);
p=p->next;
}
return *this;
}
//destructor
//returns all memory allocated to the list back to the heap when a linked lis\
//t
//object is destroyed.
//
//No parameters
//no return
template <class T>
linkedList<T>::~linkedList()
{
node *current = head;
while(current != NULL)
{
node *following = current -> next;
delete current;
current = previous;
}
head = NULL;
}
//append
//appends integer to the end of list
//Parameters: Value to append
//no return
template <class T>
void linkedList<T>::append(const linkedList &value)
{
node *add;
node *tmpNode;
if(current==NULL)
{
tmpNode=current;
add=new node;
current=add;
}
}
//begin
//sets the current position to the beginning of the lsit.
//Also sets previous to null
//No params
//No return
template <class T>
void linkedList<T>::begin()
{
current=head;
previous=NULL;
}
//retrieve
//Retrieves the value at the current position
//value is returned through the parameter
//Params: Variable for value to retrieve
//Return: True if position is valid, false otherwise
template <class T>
bool linkedList<T>::retrieve(T &value)const
{
if(current!=NULL)
{
value=current->data;
returntrue;
}
elsereturnfalse;
}
//prefix
template <class T>
linkedList<T> linkedList<T>::operator++()
{
if(current!=NULL)
{
previous=current;
current=current->next;
}
return *this;
}
//postfix
template <class T>
linkedList<T> linkedList<T>::operator++(int i)
{
linkedList<T>L=*this;
if(current!=NULL)
{
previous=current;
current=current->next;
}
return L;
}
//ostream operator for writing to file
template <class T>
ostream &operator<<(ostream &output, linkedList<T> list)
{
T thing;
list.begin();
while(list.retrieve(thing))
{
output << thing;
list++;
}
}
If you can edit your post, highlight the code portion, then click on the <> button in the Format palette at the right side of the post, that'll format your code for the forum and make it a lot easier to read :)
Edit:
If you can also copy and paste the text of the error messages you are getting, that would also be helpful.
In file included from DataStructuresAssign1.h:100,
from dataStrucApp.cpp:8:
DataStructuresAssignment1.cpp:32: error: redefinition of `linkedList<T>::linkedList(const linkedList<T>&)'
DataStructuresAssignment1.cpp:20: error: `linkedList<T>::linkedList(const linkedList<T>&)' previously declared here
DataStructuresAssignment1.cpp: In member function `linkedList<T>& linkedList<T>::operator=(const linkedList<T>&)':
DataStructuresAssignment1.cpp:52: error: there are no arguments to `clear' that depend on a template parameter, so a declaration of `clear' must be available
DataStructuresAssignment1.cpp:52: error: (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
DataStructuresAssignment1.cpp: At global scope:
DataStructuresAssignment1.cpp:88: error: prototype for `void linkedList<T>::append(const linkedList<T>&)' does not match any in class `linkedList<T>'
DataStructuresAssign1.h:48: error: candidate is: void linkedList<T>::append(const T&)
DataStructuresAssignment1.cpp:88: error: template definition of non-template `void linkedList<T>::append(const linkedList<T>&)'
dataStrucApp.cpp:17: error: variable or field `addToList' declared void
dataStrucApp.cpp:17: error: missing template arguments before '&' token
dataStrucApp.cpp:17: error: `list' was not declared in this scope
dataStrucApp.cpp:23: error: variable or field `writeList' declared void
dataStrucApp.cpp:23: error: missing template arguments before '&' token
dataStrucApp.cpp:23: error: `list' was not declared in this scope
dataStrucApp.cpp: In function `int main()':
dataStrucApp.cpp:34: error: missing template arguments before "L"
dataStrucApp.cpp:34: error: expected `;' before "L"
dataStrucApp.cpp:40: error: `L' was not declared in this scope
dataStrucApp.cpp:40: error: `FillList' was not declared in this scope
dataStrucApp.cpp:41: error: `writeList' cannot be used as a function
dataStrucApp.cpp: At global scope:
dataStrucApp.cpp:64: error: variable or field `addToList' declared void
dataStrucApp.cpp:64: error: redefinition of `int addToList'
dataStrucApp.cpp:17: error: `int addToList' previously defined here
dataStrucApp.cpp:64: error: missing template arguments before '&' token
dataStrucApp.cpp:64: error: `list' was not declared in this scope
I'm sorry, I tried to rework this in VisualStudio based on files from Putty. The file names are : DataStructuresAssign1.h, DataStructuresAssignment1.cpp and dataStrucApp.cpp.
I used different file name in the Visual Studio versions.
Also, the errors I had posted were from an earlier attempt to compile, before I used different file names
> error: redefinition of `linkedList<T>::linkedList(const linkedList<T>&)'
> error: `linkedList<T>::linkedList(const linkedList<T>&)' previously declared here
you are defining the same function twice
> error: there are no arguments to `clear' that depend on a template parameter
¿what is `clear()'? you have never declared it
> error: prototype for `void linkedList<T>::append(const linkedList<T>&)' does not match
you say that you would append one element, then go and append a list.
> error: missing template arguments before '&' token
linkedList is a template
I've fixed everything but the "missing template arguments"
A lot of this code is reused from an older project. This project, which was a linkedList class with a template of another class. This project is only using the linkedList class. I'm not sure why it is throwing an error due to a missing template argument. I have no argument to put in there.