Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类动态数组错误_C++_Arrays_Class_Logic - Fatal编程技术网

C++ 类动态数组错误

C++ 类动态数组错误,c++,arrays,class,logic,C++,Arrays,Class,Logic,我需要实现一个动态数组,该数组根据输入到代码中的值(温度)的数量进行动态调整。我已经编写了大部分代码来实现这一点,但是我遇到了一个bug,在我的一生中,一直无法找到这个问题 程序应该输出temp_a的值,使temp_b=temp_a,输出temp_b的值,然后清除temp_a的值,最后再次输出temp_b的值 但是,当我编译程序时,它输出列表已满,无法再添加任何值,这意味着代码中的某个地方存在逻辑错误 请原谅我的代码太长,一旦我能找到错误,代码将分为多个编译 #include <iost

我需要实现一个动态数组,该数组根据输入到代码中的值(温度)的数量进行动态调整。我已经编写了大部分代码来实现这一点,但是我遇到了一个bug,在我的一生中,一直无法找到这个问题

程序应该输出temp_a的值,使temp_b=temp_a,输出temp_b的值,然后清除temp_a的值,最后再次输出temp_b的值

但是,当我编译程序时,它输出列表已满,无法再添加任何值,这意味着代码中的某个地方存在逻辑错误

请原谅我的代码太长,一旦我能找到错误,代码将分为多个编译


#include <iostream>
using namespace std;

class TemperatureList {
private:
    int* temp;  // pointer to dynamic array
    short current_size;     // current number of elements
    short max_size; // max number of elements allowed in this list
public:
    // Overloading assignment operator
    void operator =(const TemperatureList& another_list);

    // === Constructors ===
    // Default constructor
    TemperatureList();
    // Constructor that accepts an integer parameter that specifies the max length of the list
    TemperatureList(int max);
    // Copy constructor that accepts another List as parameter
    TemperatureList(const TemperatureList& another_list);
    // Destructor
    ~TemperatureList();

    // === Modifier functions ===
    // add new_value to end of list if there is still space
    void add_temperature(int new_value);

    // === Accessor functions ===
    // return current current_size of the list
    short get_current_size();

    // === Other functions ===  
    // return the last element, or 0 if the list is empty, with a warning output
    int get_last();
    // return element at the position-th position, or 0 if the list is empty, with a warning output
    int get_temp(short position);
    // returns if current_size == 0
    bool set_temp(short position, int value);
    // returns if current_size == 0
    bool empty();
    // returns if current_size == max_size
    bool full();
    // Output list separated by commas
    friend ostream& operator <<(ostream& outs, const TemperatureList& list);
};

int main() {
    TemperatureList temp_a;

    temp_a.add_temperature(23.5);
    temp_a.add_temperature(24.6);
    cout << temp_a;

    TemperatureList temp_b = temp_a;
    cout << temp_b;

    temp_a = TemperatureList();
    cout << "Now there's no temperatures in a.\n";
    cout << temp_a;
    cout << "How about temperatures in b?\n";
    cout << temp_b;

    return 0;
}

void TemperatureList::operator =(const TemperatureList& another_list) {
    delete[] temp;
    current_size = another_list.current_size;
    max_size = another_list.max_size;
    if (current_size > 0) {
        temp = new int[max_size];
        for (int i = 0; i < max_size; i++) {
            temp[i] = another_list.temp[i];
        }
    }
    else {
        temp = NULL;
    }
}
TemperatureList::TemperatureList() {
    current_size = 0;
    max_size = 0;
    temp = NULL;
}
TemperatureList::TemperatureList(int max) : max_size(max) {
    current_size = 0;
    temp = new int[max];
}
TemperatureList::TemperatureList(const TemperatureList& another_list) {
    current_size = another_list.current_size;
    max_size = another_list.max_size;
    if (current_size > 0) {
        temp = new int[max_size];
        for (int i = 0; i < max_size; i++) {
            temp[i] = another_list.temp[i];
        }
    }
    else {
        temp = NULL;
    }
}
TemperatureList::~TemperatureList() {
    //cout << "== I am in destructor ==\n";
    delete[] temp;
}

void TemperatureList::add_temperature(int new_value) {
    if (current_size < max_size) {
        temp[current_size] = new_value;
        current_size++;
    }
    else {
        cout << "Cannot add value to the list. It is full.\n";
    }
}
int TemperatureList::get_last() {
    if (empty()) {
        cout << "The list is empty\n";
        return 0;
    }
    else {
        return temp[current_size - 1];
    }
}
int TemperatureList::get_temp(short position) {
    if (current_size >= position) {
        return temp[position - 1];
    }
    else {
        cout << "There is no temperature\n";
        return 0;
    }
}
bool TemperatureList::set_temp(short position, int value) {
    if (current_size >= position) {
        temp[position - 1] = value;
        return true;
    }
    else {
        return false;
    }
}
short TemperatureList::get_current_size() {
    return current_size;
}
bool TemperatureList::empty() {
    return (current_size == 0);
}
bool TemperatureList::full() {
    return (current_size == max_size);
}
ostream& operator <<(ostream& outs, const TemperatureList& list) {
    int i;
    for (i = 0; i < (list.current_size - 1); i++) {
        outs << list.temp[i] << ",";
    }
    outs << list.temp[i];
    return outs;
}

#包括
使用名称空间std;
阶级温度学家{
私人:
int*temp;//指向动态数组的指针
短电流_size;//当前元素数
short max_size;//此列表中允许的最大元素数
公众:
//重载赋值运算符
void运算符=(const TemperatureList&另一个列表);
//==构造函数===
//默认构造函数
温度学家();
//接受指定列表最大长度的整数参数的构造函数
温度列表(int max);
//复制接受另一个列表作为参数的构造函数
温度列表(常数温度列表和另一个列表);
//析构函数
~TemperatureList();
//==修饰符函数===
//若仍有空间,则在列表末尾添加新的_值
无效添加温度(int新值);
//==访问器函数===
//返回列表的当前大小
短获取当前大小();
//==其他函数===
//返回最后一个元素,如果列表为空,则返回0,并显示警告输出
int get_last();
//返回第th个位置的元素,如果列表为空,则返回0,并输出警告
int get_temp(空头位置);
//如果当前_大小==0,则返回
布尔设置温度(短位置,int值);
//如果当前_大小==0,则返回
bool empty();
//如果当前大小==最大大小,则返回
bool-full();
//以逗号分隔的输出列表

friend ostream&operator逻辑错误似乎源于您将当前大小和最大大小初始化为零。因此,除非您运行重载构造函数(其中设置了最大大小),否则每次调用addTemperature()都会失败(当前大小<最大大小)检查,因为它们都等于零。

可能对您来说,这段代码输出的列表已满,但对我来说,您发布的代码崩溃了,因为您正在使用默认构造函数构造温度列表,该构造函数将
temp
设置为null。如果
temp
nullptr
,您尝试并遵从它(
temp[n]
*temp
temp->
)您将遇到分段错误,程序将崩溃。如果在其中一些方法中添加
temp
的空检查,程序将更加健壮。