<> Compiler and Linker errors for my linkedList class and application

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.

**************************header*************************
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>
using namespace 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



*******************implementation**********************
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
using namespace 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;
      return true;
    }
  else
    return false;
}

//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++;
      }
}


****************application***********
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "dataStructuresHeader.h"
#include <iostream>
#include <fstream>
using namespace std;

//AddToList
//Description: appends to the list
//Parameters: linkedList
//Return: None
void addToList(linkedList &list);

//writeList
//Description: Writes integers to txt
//Parameters: linkedList
//return: none
void writeList(linkedList &list);


//Menu

int menu();

int main()
{

  int choice;
  linkedList L;

  while ((choice = menu()) != 3)
    {
      switch (choice)
        {
        case 1: addToList(L); break;
        case 2: writeList(L); break;
        }
    }
  return 0;
}

int menu()
{
  int ch;
  cout<<endl;
  cout<<"1. Fill from file"<<endl;
  cout<<"2. Write to file"<<endl;
  cout<<"3. Exit"<<endl;
  cout<<"Choice: ";
  cin>>ch;
  return ch;

}
//AddToList
//Description: opens file and fills list with contents
//Parameters: linkedlist
//return: none
void addToList(linkedList &list)
{
  int value;
  ifstream f;
  string fname;
  int temp;

  cout << "File: ";
  cin >> fname;
  f.open(fname.c_str());
  if (f.fail())
    {
      cout << "Failed to open" << endl;
      return;
    }

  f >> temp;

  while( f >> temp )
    {
      list.begin();
      while (list.retrieve(value) & value < temp)
        ++list;

      list.insert(temp);
	     f>>temp;
    }
  f.close();

}
Last edited on
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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
Are there only supposed to be 3 files? The file names in the errors don't seem to match up with the code you've posted.

Your main program includes this

#include "dataStructuresHeader.h"

but the error messages refer to a DataStructuresAssign1.h

The header code you posted has this for the implementation cpp

#include "dataStrucImp.cpp"

but the error messages refer to these
DataStructuresAssignment1.cpp
and
dataStrucApp.cpp
Last edited on
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


Last edited on
http://www.eelis.net/iso-c++/testcase.xhtml
Make sure any line numbers you mention in the testcase are correct

> 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'm sorry
if you are sorry, go and fix it.
Last edited on
How should the template arguments be declared?

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.
Last edited on
Topic archived. No new replies allowed.