我收到错误“表达式必须具有指向类的指针”。我已经搜索了错误并且无法找到一个我可以弄清楚发生了什么的帖子。我在第49行(data->resize( capacity * 2 );
)
Sequence2.h
typedef double value_type;
class sequence
{
private:
value_type* data;
int used;
int current_index;
int capacity;
public:
// TYPEDEFS and MEMBER CONSTANTS
static const int DEFAULT_CAPACITY = 5;
// CONSTRUCTORS and DESTRUCTOR
sequence(int initial_capacity = DEFAULT_CAPACITY);
sequence(const sequence& source);
~sequence();
// MODIFICATION MEMBER FUNCTIONS
void insert(const value_type& entry);
void resize(int new_capacity);
// ADDITIONAL FUNCTIONS THAT ARE NOT RELEVANT
Sequence2.cpp
#include "stdafx.h"
#include "Sequence2.h"
// Postcondition: The sequence has been initialized as an empty sequence.
// The insert/attach functions will work efficiently (without allocating
// new memory) until this capacity is reached.
sequence::sequence(int initial_capacity)
{
capacity = initial_capacity;
current_index = -1;
data = new value_type[DEFAULT_CAPACITY];
}
sequence::sequence(const sequence & source)
{
// NEED TO DO
// Postcondition: the sequence has made a deep copy of the source sequence.
}
sequence::~sequence()
{
delete[] data;
}
// Postcondition: A new copy of entry has been inserted in the sequence
// before the current item. If there was no current item, then the new entry
// has been inserted at the front of the sequence. In either case, the newly
// inserted item is now the current item of the sequence.
void sequence::insert(const value_type & entry)
{
if (current_index < 0)
{
current_index = 0; // Set current index to first index
data[current_index] = entry;
used++;
}
else
{
if (used < capacity) {
data[current_index + 1] = entry;
current_index++;
used++;
}
else
{
data->resize( capacity * 2 );
}
}
}
void sequence::resize(int new_capacity)
{
value_type *temp = new value_type[capacity];
int temp_capacity = capacity;
for (int i = 0; i < capacity; i++)
{
temp[i] = data[i];
}
delete[] data;
data = new value_type[new_capacity];
for (int i = 0; i < temp_capacity; i++)
{
data[i] = temp[i];
used++;
current_index++;
}
delete[] temp;
}
答案 0 :(得分:1)
resize
也是会员功能,您没有正确调用它。变化
data->resize( capacity * 2 );
到
resize( capacity * 2 );
此处还有其他一些问题:
您可能需要在resize()
中调用insert()
后插入值。
new
中无需delete
/ resize()
两次。
used
之后current_index
和resize()
的值似乎有误。