#include <iostream>
using namespace std;
template <typename T, int length>
class SortedList
{
private:
ostream& put(ostream& out) const;
T list[length];
int size;
public:
SortedList()
{
size = 0;
}
void insert(T item);
friend ostream& operator<<(ostream& out, const SortedList& list)
{
return list.put(out);
}
};
template<typename T, int length>
void SortedList<T, length>::insert(T item)
{
if (size == length) {
printf("List Full"); return;
}
int i = size - 1;
while (i >= 0 && item < list[i])
{
list[i + 1] = list[i];
i--;
}
list[i + 1] = item;
size++;
}
template<typename T, int length>
ostream& SortedList<T, length>::put(ostream& out) const
{
for (int i = 0; i < size; i++)
cout << list[i] << " ";
cout << endl;
return out;
}
class Int
{
public:
Int(int i) { this->i = i; }
private:
int i;
};
int main()
{
Int values[] = { Int(5), Int(1), Int(7), Int(8), Int(11), Int(2) };
SortedList<Int, 10> list;
for (int i = 0; i < 6; i++)
list.insert(values[i]);
cout << list;
return 0;
}
Why doesn't the code work?
Not sure if this a copy paste issue but in C++ it's int not Int
Sorry, didn't see the class Int.
Last edited on
Why do you have a class Int? Why not use the built-in int type directly? When I do this, the program works.
Last edited on
The problem is that the class Int doesn't have a default ctor, which is needed in the LikedList class
T list[length];