gpt4 book ai didi

c++ - 接收错误: expression must have pointer-to-class type

转载 作者:行者123 更新时间:2023-11-30 05:33:34 25 4
gpt4 key购买 nike

我收到错误“表达式必须具有指向类的指针类型”。我已经搜索了错误,但未能找到可以弄清楚发生了什么的帖子。我在第 49 行收到关于 Sequence2.cpp 的错误 (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;

}

最佳答案

resize 也是一个成员函数,您没有正确调用它。改变

data->resize( capacity * 2 );

resize( capacity * 2 );

还有一些其他问题:

  1. 您可能需要在 insert() 中调用 resize() 之后插入值。

  2. 无需在 resize()new/delete 两次。

  3. resize() 之后,usedcurrent_index 的值似乎不对。

关于c++ - 接收错误: expression must have pointer-to-class type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34714504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com