Template List class wont display insertEnd.

Dear All,

I am no where near a genius and most of this stuff jsut confuses me. Ive been seeking tutoring but I can't seem to find any of my mistakes(probably doesn't help that Im 3 or 4 assignments behind). Need some help with my code and will post the assignment so its easier to follow. Ill put the assignment in quotes:

1)Define the ‘<’ and ‘>’ operators for the SmallPacket and LargePacket classes from assignment 1. SmallPackets should be ordered based on the ‘value,’ and LargePackets based on the ‘height.’

2)Implement a template List class, using a dynamically allocated array, as discussed in the lecture. The initial size of the array should be passed as an argument to the constructor. The class should be tested with values of type ‘int’, then of type of ‘SmallPacket,’ then of type ‘LargePacket.’
The class should contain the following member functions:

Constructor
Takes a single int argument used to specify the initial size of the storage. If the size is not passed as an argument, the initial size of the storage should be 20.

Destructor
Ensures that there are no memory leaks.

void display() const
Displays all values currently stored in the list.

void displayReverse() const
Displays all values currently stored in the list, in reverse order.

void insertFront(const T& num)
Takes a single argument representing the value to be inserted into the front of the list. If the list is already full, the size of the storage should be doubled and the new value inserted. All previously inserted values should be retained in the same order.

void insertEnd(const T& num)
Takes a single argument representing the value to be inserted at the end of the list. If the list is already full, the size of the storage should be doubled and the new value inserted. All previously inserted values should be retained in the same order.

void insertInOrder(const T& num)
Takes a single argument representing the value to be inserted into the list. Following this insertion, values should be stored in the list in ascending order. If the list is already full, the size of the storage should be doubled and the new value inserted. All previously inserted values should be retained in the same order.

If this function is called on a list, do not call either of the other insert functions on the same list
int getSize() const
Returns the current size of the list.

int getNumValues() const
Returns the current number of values stored in the list.

T getValue(int index) const
Returns the value at the specified index in the list.




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

using namespace std;

template <typename T>
List<T>::List(int s)
{
	size = s;
	numValues = 0;
	values = new T[size];
}

template <typename T>
void List<T>::insertFront(const T& num)
{
}
template <typename T>
void List::insertEnd(const T& num)
{
        values[numValues] = num;
        ++numValues;
}
template <typename T>
void List::displayReverse() const
{
}
template <typename T>
void List::insertInOrder(const T& num)
{
}
template <typename T>
void List::display()const
{
	

}
template <typename T>
List::~List()
{
	delete [] value;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "List.h"
#include "SmallPacket.h"
#include "LargePacket.h"
#include <string>
#include <iostream>

int main()
{
	List<int> L1;
//	SmallPacket s1(5.5, 3.4);
//	SmallPacket s2(2.7, 1.4);

	L1.insertEnd(10);
	L1.insertEnd(8);

	L1.display();

}


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


#include <string>

template <typename T>
class List 
{
   public:
	   List(int s=20);
	   ~List();
       int getSize() const;
	   int getNumValues() const;
       void display() const;
       void displayReverse() const;
	   void insertFront(const T& num);
	   void insertEnd(const T& num);
	   void insertInOrder(const T& num);
   private:
	   T* value;
       int size;
	   int numValues;
};
#include "list.cpp"
#endif 



those 3 are what I have so far for me list. As of now Im only trying to get insertEnd to display but can;t find the code to do so(and its fairly easy code I believe, seeing as this is a basic college class, but alas Im not a genius and the book tells me nothing). Here are the small packet and large packet codes just in case anyone wants to fool around with it. Any and all help would be greatly appreciated.



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
#include "LargePacket.h"
#include <iostream>

using namespace std;


LargePacket::LargePacket(double wd, double ht)
{
   
    setWidth(wd);
    setHeight(ht);
   
}

void LargePacket::setWidth(double wd)
{
   if (wd < 0)
   {
     wd = 0;
   }
   width = wd;
}

void LargePacket::setHeight(double ht)
{
   if (ht < 0)
   {
      ht = 0;
   }
   height = ht;
   
}
 
void LargePacket::display() const
{
    cout << "Width:  " << getWidth() << endl;
    cout << "Height:    " << getHeight() << endl;

}


double LargePacket::getWidth() const
{
    return width;
}

double LargePacket::getHeight() const
{
    return height;
}

std::ostream& operator <<(std::ostream& os, const LargePacket& lp)
{
	lp.display();
	return os;
}


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

#include <string>

class LargePacket 
{
    public:
    LargePacket(double wd = 0.0, double ht = 0.0);
    void setWidth(double wd);
    void setHeight(double ht);
    double getWidth() const;
    double getHeight() const;
    void display() const;
   


private:
    double width;
    double height;

};

std::ostream& operator <<(std::ostream& os, const LargePacket& lp);

#endif 


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
#include "SmallPacket.h"
#include <iostream>

using namespace std;


SmallPacket::SmallPacket(double wt, double val)
{
   
    setWeight(wt);
    setValue(val);
   
}

void SmallPacket::setWeight(double wt)
{
   if (wt < 0)
   {
     wt = 0;
   }
   weight = wt;
}

void SmallPacket::setValue(double val)
{
   if (val< 0)
   {
      val = 0;
   }
   value = val;
   
}
 
void SmallPacket::display() const
{
    cout << "Weight:  " << getWeight() << endl;
    cout << "Value:    " << getValue() << endl;

}


double SmallPacket::getWeight() const
{
    return weight;
}

double SmallPacket::getValue() const
{
    return value;
}

std::ostream& operator <<(std::ostream& os, const SmallPacket& sp)
{
	sp.display();
	return os;
}



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

#include <string>

class SmallPacket 
{
    public:
    SmallPacket(double wt = 0.0, double val = 0.0);
    void setWeight(double wt);
    void setValue(double val);
    double getWeight() const;
    double getValue() const;
    void display() const;
   


private:
    double weight;
    double value;

};

std::ostream& operator <<(std::ostream& os, const SmallPacket& sp);

#endif 
Hi!
I guess the first code is list.cpp. This one must #include "list.h", not vice versa. Also in this one u must correct every function with List<T>::....
In main.cpp(or whatever its name is) u must #include "list.cpp" (witch will include "list.h" itself), #include "smallpacket.cpp"(will include "smallpacket.h" itself) and "largepacket.cpp".
After that at least u get rid of the errors. I didn't checked the result of the program, just the compiling process.
Regards!
Topic archived. No new replies allowed.