I am defining and implementing a list with arrays for class. In operations that prints a list, ej rest
(returns a list with all the elements you put in except the first), the following happens:
The letter is the operation, the first number is the number of elements, the rest of the numbers is the list example
When you press enter ... the infinity
This is the code, divided into several files:
Array_list.hpp (summary):
#ifndef ARRAY_LIST_HPP
#define ARRAY_LIST_HPP
#include <stdexcept>
template<typename E, int N>
class ArrayList
{
public://there are more operations
ArrayList();//builder
void rest(ArrayList& rest);
private:
E store_ [N];
int finalPos_;//the final pos in the array with an element
};
#include "array_list.cpp"
#include "array_list_io.cpp"
#endif // ARRAY_LIST_HPP
array_list.cpp(there are more operations)
//builder
template<typename E, int N>
ArrayList<E,N>::ArrayList()
{
finalPos_=-1;// empty list
}
template<typename E, int N>
void ArrayList<E,N>::rest(ArrayList<E,N>& rest)
{
for(int i=1; i<=finalPos_;i++)
{
rest.store_[i]=store_[i];
}
}
Test.cpp (This is where you define and implement the opening and closing of files)
#include "array_list.hpp"
#define Element int
#define TestList ArrayList<Element, 10>
void testRest(std::istream& is)
{
TestList l;
TestList r;
is >> l;
l.rest(r);
std::cout << r << std::endl;
std::cout << l;
}
int main()
{
char option;
// Lectura de la operación a probar
is >> opcion;
switch(opcion)
{//These are all operations that I do not add here by their extension
case 'c': testBuild(is); break;
case 'p': testFirst(is); break;
case 'r': testRest(is); break;
case 'v': testIsEmpty(is); break;
case 'l': testLong(is); break;
case 'u': testLatest(is); break;
case 'P': testBelong(is); break;
case 'i': testFinalInsert(is); break;
case 't': testConcatenate(is); break;
case 'b': testEraseElement(is); break;
case 'C': testCopy(is); break;
case '=': testEqual(is); break;
}
return 0;
}
array_list_io.cpp
template<typename T, int N>
std::ostream& operator<<(std::ostream& os, ArrayList<T, N>& l)
{
T e;
ArrayList<T, N> copy;
os << l.length();
if (!l.isEmpty())
{
os << " ";
}
copy = l;
while (!copy.isEmpty())
{
copy.first(e);
os << e;
if (copy.length()>1)
{
os << " ";
}
copy.rest(copy);
}
return os;
}
template<typename T, int N>
std::istream& operator>>(std::istream& is, ArrayList<T, N>& l)
{
T e;
int length;
is >> length;
for (int i=0; i< length; i++)
{
is >> e;
l.FinalInsert(e);
}
return is;
}
What can be the error?
Aucun commentaire:
Enregistrer un commentaire