undefined reference to 'List::List()'

Hey everyone I'm trying to do a program where I overload operators such as *, +, / etc. so that I can add/multiply/subtract vectors/lists from each other and add numbers to them etc. I keep getting an error in my main function though when I go to create a list (List aList;) and the compiler says undefined reference to 'List::List()' I can't figure out why it won't let me create a list since I've included the header files.

Main:

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
#include <iostream>
#include <string>
#include <exception>
#include "list.h" // ADT list operations
#include "listExceptions.h"

using namespace std;

int main()
{
   List aList;
   ListItemType dataItem;
   bool         success;

   try {
       aList.insert(1, 20);
   }
   catch( ListException & e1)
   {
          cerr << e1.what();
   }
   catch ( ListIndexOutOfRangeException & e2)
   {
       //handling the exception here
   }
   catch (...)
   {
         //handling all the other exceptions here
   }
   aList.retrieve(1, dataItem);

   cout <<"The item at position 1 is: "<< dataItem <<endl;

   return 0;

}


List.cpp

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
#include "list.h"  // header file

List::List(): size(0)
{

}  // end default constructor

bool List::isEmpty() const
{
   return size == 0;
}  // end isEmpty

int List::getLength() const
{
   return size;
}  // end getLength

void List::insert(int index, const ListItemType& newItem)
   throw(ListIndexOutOfRangeException, ListException)
{
   ListException listEx("ListException: List full on insert");
   ListIndexOutOfRangeException outOfRange("ListIndexOutOfRangeException: Bad index on insert");
   if (size >= MAX_LIST)
      throw listEx;
      //ListException("ListException: List full on insert");

   if (index >= 1 && index <= size+1)
   {
       for (int pos = size; pos >= index; --pos)
         items[translate(pos+1)] = items[translate(pos)];
      // insert new item
      items[translate(index)] = newItem;
      ++size;  // increase the size of the list by one
   }
   else  // index out of range
      throw outOfRange;
      //ListIndexOutOfRangeException( "ListIndexOutOfRangeException: Bad index on insert");
   // end if
}  // end insert


void List::remove(int index)
     throw(ListIndexOutOfRangeException)
{
   bool success;
   ListIndexOutOfRangeException outOfRange;

   success = (index >= 1) && (index <= size);

   if (!success) throw outOfRange;

   if (success)
   {  // delete item by shifting all items at positions >
      // index toward the beginning of the list
      // (no shift if index == size)
      for (int fromPosition = index + 1;
	   fromPosition <= size;
	   ++fromPosition)
         items[translate(fromPosition - 1)] = items[translate(fromPosition)];
      --size;  // decrease the size of the list by one
   }  // end if

}  // end remove

void List::retrieve(int index, ListItemType& dataItem) const
     throw(ListIndexOutOfRangeException)
{
   bool success;
   ListIndexOutOfRangeException outOfRange;

   success = (index >= 1) && (index <= size);

   if (!success) throw outOfRange;

   if (success)
      dataItem = items[translate(index)];
}  // end retrieve

int List::translate(int index) const
{
    return index-1;
}


 List operator + (const List& rhs) const{

    List sum_vector;
    for(int i=0;i<size; i++){
        sum_vector.items[ i ] = (*this).items[i] + rhs.items[i];
    }
    return sum_vector;
 }


 List operator - (const List& rhs) const{

    List difference_vector;
    for(int i=0;i<size; i++){
        difference_vector.items[i] = (*this).items[i] - rhs.items[i];
    }
    return difference_vector;

 }


 List operator * (const List& int num) const{

    List mul_vector;
    for(int i=0;i<size; i++){
        mul_vector.items[i] = (*this).items[i] * num;
    }

    return mul_vector;

 }

 List operator / (const List& int num) const{

    List div_vector;
    for(int i=0;i<size; i++){
        div_vector.items[i] = (*this).items[i] / num;
    }

    return div_vector;
 }

List operator ++ (          ) const { //prefix

    List prefix_vector;
    for(int i=0;i<size; ++i){
        prefix_vector.items[i] = (*this).items[i] +1;
    }
    return prefix_vector;
 }

List operator ++ (      int useless) const { //postfix

    List postfix_vector;
    for(int i=0;i<size; i++){
        postfix_vector.items[i] = (*this).items[i] +1;
    }
    return postfix_vector;
 }

List operator << (const List& vector){

    for(int i=0;i<size; i++){
        cout << vector.items[i] << " ";
    }
    return vector;

 }


list.h

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
/** @file ListAexcept.h */
#ifndef _LIST_H
#define _LIST_H

#include "listExceptions.h"

const int MAX_LIST = 20;
typedef int ListItemType;

/** @class List
 * ADT list - Array-based implementation with exceptions */
class List
{
public:
   List();
   // destructor is supplied by compiler

   /** @throw None. */
   bool isEmpty() const;

   /** @throw None. */
   int getLength() const;

   /** @throw ListIndexOutOfRangeException  If index < 1 or index >
    *         getLength() + 1.
    *  @throw ListException  If newItem cannot be placed in the list
    *         because the array is full. */
   void insert(int index, const ListItemType& newItem)
      throw(ListIndexOutOfRangeException, ListException);

   /** @throw ListIndexOutOfRangeException  If index < 1 or index >
    *         getLength(). */
   void remove(int index)
      throw(ListIndexOutOfRangeException);

   /** @throw ListIndexOutOfRangeException  If index < 1 or index >
    *         getLength(). */
   void retrieve(int index, ListItemType& dataItem) const
        throw(ListIndexOutOfRangeException);

   List operator << (const List& vector) const;
   List operator ++ (      int useless) const;
   List operator ++ (          ) const;
   //List operator / (const List& int num) const;
   //List operator * (const List& int num) const;
   List operator - (const List& rhs) const;
   List operator + (const List& rhs) const;


private:
   /** array of list items */
   ListItemType items[MAX_LIST];

   /** number of items in list */
   int          size;

   int translate(int index) const;
}; // end List
// End of header file.

#endif 


listExceptions.h

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
#ifndef _LIST_EXCEPTIONS_H
#define _LIST_EXCEPTIONS_H

#include <stdexcept>
#include <string>

using namespace std;

class ListIndexOutOfRangeException : public out_of_range
{
public:

   /**/
   ListIndexOutOfRangeException(const string & message = "")
      : out_of_range(message.c_str())
   { }  // end constructor
   /**/
   //virtual const char* what() const throw()
   //{return "list: out of range";}
}; // end ListIndexOutOfRangeException


class ListException : public logic_error
{
public:

   /**/
   ListException(const string & message = "")
      : logic_error(message.c_str())
   { }  // end constructor
   /**/
   // virtual const char* what() const throw()
   //{return "list: logic error";}   //list is full
}; // end ListException

#endif 
any help is really appreciated...i also tried to use this in my files

#include<list>
closed account (DSLq5Di1)
I just copy'pasted it into ideone ( http://ideone.com/QeqYe ), a couple of minor issues needed resolving, nothing related to the constructor.

List.cpp
- Missing class scope declarations for member functions, from line 85 down.

- Parameters for List::operator* & List::operator/ (const List& int num) // ?? . I see you've commented these functions out in the header, you may wish to do so in the cpp file too.

- List::operator<< declaration in the header does not match the definition, missing the const qualifier.


A couple of the overloaded operators are questionable, I recommend you thoroughly read through:-
http://stackoverflow.com/questions/4421706/operator-overloading
Last edited on
this is helpful thank you
Topic archived. No new replies allowed.