I'm trying to modify a class (i.e. I'm hoping it's one small thing that I forgot to change and not actually 38 errors) to use dynamic arrays rather than a static array. However, when compiling, I get 38 (new record!) errors in my program. A lot of them seem to be related to undeclared identifiers:
Simply put, these seem like odd errors to be getting. Both of these were just typedef statements in the header file given to us. Where am I going wrong?
Here's the implementation file (this was written by me, so look for the error in here. I'll include the header file for the sake of reference):
#include <iostream> // provides cout
#include <cassert> // provides assert
#include <cstdlib> // provides size_t
#include "sequence2.h"
usingnamespace std;
namespace main_savitch_4
{
sequence::sequence(size_type initial_capacity)
{
data = new value_type[initial_capacity]; // Creates dynamic array pointed to by data,
capacity = initial_capacity; // of size initial_capacity
current_index = 0;
used = 0;
}
sequence::sequence(const sequence& source)
{
data = new value_type[source.capacity]; // Creates dynamic array pointed to by data,
capacity = source.capacity; // of size source.capacity
used = source.used;
current_index = source.current_index;
for(size_type i = 0; i<used; ++i)
{
data[i]=source.data[i]; // Copies array from array pointed to by source.data
} // to array pointed to by data
}
sequence::~sequence( )
{
delete [ ] data;
}
void reserve(size_type new_capacity)
{
value_type *larger_array;
if (new_capacity == capacity)
return; // The allocated memory is already the right size.
if (new_capacity < used)
new_capacity = used; // Can’t allocate less than we are using.
larger_array = new value_type[new_capacity];
for(size_type i = 0; i<used; ++i) // Copy data from array pointed to by data to
{ // array pointed to by larger_array
larger_array[i] = data[i];
}
delete [ ] data;
data = larger_array;
capacity = new_capacity;
}
void sequence::advance()
{
assert(is_item()); // Changes current item to next item in sequence
++current_index; // as long as current item is a valid item
}
void sequence::insert(const value_type& entry)
{
if(used == capacity) // If dynamic array is currently full, increase size
reserve(used+used/10+1); // by at least 10%
if (current_index == used) // If there is no current item, insert new item
current_index = 0; // at the beginning of the sequence
for (size_t i = used; i > current_index; --i)
{
data[i] = data[i-1]; // Shift all items in the array from current_index
} // to used one space forward
data[current_index] = entry; // Insert new entry at current_index,
++used; // increase count of items in array
return;
}
void sequence::attach(const value_type& entry)
{
if(used == capacity) // If dynamic array is currently full, increase size
reserve(used+used/10+1); // by at least 10%
if (current_index == used) // If there is no current item, add item at
{ // current_index and increase used
data[current_index] = entry;
++used;
return;
}
for (size_t i = used; i > current_index+1; --i)
{ // Shift items in array from current_index+1 to used
data[i] = data[i-1]; // one space forward
}
++current_index; // Increase current_index to identify correct
++used; // current item, increase count of items in array,
data[current_index] = entry; // insert new entry at current_index
return;
}
void sequence::remove_current()
{
assert (is_item()); // Ensure current_index refers to a valid item
if(current_index == used-1) // If current item is the last item in the
{ // sequence, reduce used by 1 to shorten
--used; // sequence.
return;
}
for(size_t i = current_index; i < used-1; ++i)
{ // Shifts items in array beyond current index
data[i] = data[i+1]; // one space backward, reduce used by 1 to
} // shorten sequence
--used;
return;
}
voidoperator=(const sequence& source)
{
value_type *new_data;
// Check for possible self-assignment
if (this == &source)
return;
// If needed, allocate an array with a different size
if (capacity != source.capacity)
{
new_data = new value_type[source.capacity];
delete [ ] data;
data = new_data;
capacity = source.capacity;
}
// Copy data from the source array
used = source.used;
for(size_type i = 0; i<used; ++i)
{
data[i] = source.data[i];
}
current_index = source.current_index;
}
sequence::value_type sequence::current() const
{
assert (is_item());
return data[current_index];
}
}
hanst99 is right - since those typedefs are defined inside the class, the only way to use them outside of the class is with a using directive or the :: operator. You could define them in the namespace if that is not what you wanted.
The other problem preventing it from compiling is this: void reserve(size_type new_capacity) and this: voidoperator=(const sequence& source)
You are just missing "sequence::" before both function names.
Ah, that's it. As Bran says, some of the functions implementations don't have sequence:: in front of them, so the compiler isn't automatically checking inside the class for those typedefs.