Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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/3/arrays/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_Qt - Fatal编程技术网

C++ 为数组元素赋值后程序崩溃

C++ 为数组元素赋值后程序崩溃,c++,arrays,qt,C++,Arrays,Qt,我有一个名为setaddress的类,它包含一个包含2D数组的结构:int WaterMeterIDs[20][2] namespace Ui { class SetAddress; } class SetAddress : public QDialog { Q_OBJECT public: struct AddressList{ int WaterMeterIDs[20][2]; }; explicit SetAddress(QWidget

我有一个名为setaddress的类,它包含一个包含2D数组的结构:
int WaterMeterIDs[20][2]

namespace Ui {
class SetAddress;
}

class SetAddress : public QDialog
{
    Q_OBJECT

public:
    struct AddressList{
        int WaterMeterIDs[20][2];
    };
    explicit SetAddress(QWidget *parent = 0);
    ~SetAddress();
    etc...
private:
    Ui::SetAddress *ui;
    AddressList m_address;
我想使用以下命令保存qtablewidget单元格中的数据 在my.cpp文件中:

void SetAddress::on_pushButton_apply_clicked()
{
    int rowscount = ui->tableWidget->rowCount();
//rowscount is always less than 20
    for(int j = 0; j < 2; j++){
        for(int i = 0; i < rowscount; i++){
            if(ui->tableWidget->item(i,j) != 0x0 ){//if cell is not empty
                 m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();//convert data to int and put it in array
                 qDebug()<<m_address.WaterMeterIDs[i][j];
            }
        }
    }
}
void SetAddress::在按钮上单击应用()
{
int rowscount=ui->tableWidget->rowCount();
//rowscount总是少于20
对于(int j=0;j<2;j++){
for(int i=0;itableWidget->item(i,j)!=0x0){//如果单元格不为空
m_address.WaterMeterIDs[i][j]=ui->tableWidget->item(i,j)->text().toInt();//将数据转换为int并放入数组中
qDebug()

你能看出我的密码有什么错误吗

即使您确定您的索引在范围内,您向我们展示的代码中也没有任何东西能够真正确保这一点,因此我将用以下内容替换您的数组:

#include <array>

class AddressList{
    std::array<std::array<int, 2>, 20> WaterMeterIDs;

public:
    inline constexpr int& at(size_t row, size_t col) {
        return WaterMeterIDs.at(row).at(col);
    }

    inline constexpr int const& at(size_t row, size_t col) const {
        return WaterMeterIDs.at(row).at(col);
    }
};
这样可以确保2D阵列没有任何漏洞

我会在循环之前检查
ui->tableWidget->columnCount()>=2
,只是为了排除这个问题:

int colcount = std::min(2, ui->tableWidget->columnCount());
for(int j = 0; j < colcount; ++j) {
   ...
int colcount=std::min(2,ui->tableWidget->columnCount());
对于(int j=0;j
你能在valgrind下运行你的程序来检查内存访问吗?在debugger中运行它,看看它挂起了,然后发生了什么……你确定tableWidget有2列20行吗?@ΦXocę웃Пepeúpaツ是的(少于20行)你能看到我的代码中有错误吗?为什么不干脆
WaterMeterIDs.at(row).at(col)
?@Caleth我怀疑它可能会在更多的地方使用,所以它只是为了更容易转换-当/如果OP想取消耗时的检查时,他可以只注释掉
检查范围
,让代码保持原样。编辑:啊,我想你的意思是,为什么我没有在
检查范围
中使用它?`好问题:-)我会怀疑
在未抛出的
方法中。您可能需要一些其他名称用于未经检查的访问,例如
int&operator()(size\u t,size\u t)
std::array&operator[](size\u t)
int&operator[](index\t)
我很困惑。我想在我的代码中找出我的错误,以防止重复。@reza moslemi您显示的代码没有显示我能看到的任何错误,只是没有检查边界。您尝试过我提出的建议吗?
int colcount = std::min(2, ui->tableWidget->columnCount());
for(int j = 0; j < colcount; ++j) {
   ...