P
pat270881
hello,
I have to implement a sequence class, however the header file is
predefined
Unfortunately, I guess something is wrong with the copy constructor, or
the insert, attach or resize function:
Here the insert, attach and resize function:
Has anybody an idea what went wrong here? I am working on the problem
fixing for such a long time but I haven't found the error yet...(
pat
I have to implement a sequence class, however the header file is
predefined
Code:
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
static const size_type DEFAULT_CAPACITY = 30;
// CONSTRUCTORS and DESTRUCTOR
sequence(size_type initial_capacity = DEFAULT_CAPACITY);
sequence(const sequence& source);
~sequence( );
// MODIFICATION MEMBER FUNCTIONS
void resize(size_type new_capacity);
void start( );
void advance( ); //set the current_index to the next number in
the array
void insert(const value_type& entry); //insert before the
number with the current index
void attach(const value_type& entry); //insert after the number
with the current index
void remove_current( );
void operator =(const sequence& source);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool is_item( ) const;
value_type current( ) const;
private:
value_type* data; //das array mit den zahlen drinnen
size_type used; //wieviele zahlen in dem array stehen
size_type current_index;
size_type capacity;
};
Unfortunately, I guess something is wrong with the copy constructor, or
the insert, attach or resize function:
Code:
sequence::sequence(const sequence& source)
{
data = new value_type[source.capacity];
capacity = source.capacity;
used = source.used;
current_index = source.current_index;
copy(source.data, source.data + used, data);
}
Here the insert, attach and resize function:
Code:
void sequence::insert(const value_type& entry)
{
if(used == capacity)
resize(used);
if(!(is_item()))
current_index = 0;
for(int i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = entry;
++used;
}
void sequence::attach(const value_type& entry)
{
if(used == capacity)
resize(used);
if(current_index >= used)
{
start();
advance();
advance();
}
if(used!=0)
{
for(int i = used; i > current_index+1; i--)
{
data[i] = data[i-1];
}
data[current_index+1] = entry;
current_index++;
}
else
{
data[current_index] = entry;
}
++used;
}
void sequence::resize (size_type new_capacity)
{
value_type* larger_array;
if(new_capacity < used)
new_capacity = used;
larger_array = new value_type[new_capacity];
copy(data, data + used + current_index, larger_array);
delete[] data;
data = larger_array;
capacity = new_capacity;
}
Has anybody an idea what went wrong here? I am working on the problem
fixing for such a long time but I haven't found the error yet...(
pat