You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用动态生成数组的C++类:二维字符串数组元素修改问题

二维字符串数组类的传参修复方案

问题分析

你的代码存在几个核心问题:

  • 静态二维数组string chart[7][7]的类型是string (*)[7][7],无法直接传递给string**类型的参数,二者内存布局和类型完全不同
  • 类成员chart是未初始化的野指针,构造函数中**chart = **arr;直接解引用会触发未定义行为(大概率崩溃)
  • 若后续要使用动态创建的二维数组,需要明确其内存结构:动态二维数组是string**(指向指针数组的指针,每个子指针指向string数组)

修正后的代码示例

方案1:适配动态二维数组(符合你后续需求)

#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm> // 用于swap函数

using namespace std;

class Example {
    int x, y;
    string** chart; // 对应动态二维数组的类型
public:
    Example(int i, int j, string** arr);
    void print() const { cout << "索引(" << x << "," << y << ")的值:" << chart[x][y] << endl; }
    // 元素交换功能实现
    void swapElement(int targetX, int targetY) {
        swap(chart[x][y], chart[targetX][targetY]);
    }
};

Example::Example(int i, int j, string** arr) {
    x = i;
    y = j;
    chart = arr; // 直接保存数组指针,而非解引用赋值
}

// 动态创建二维字符串数组的工具函数
string** createDynamicArray(int rows, int cols) {
    string** arr = new string*[rows];
    for (int i = 0; i < rows; ++i) {
        arr[i] = new string[cols];
        // 初始化空元素为三个空格
        for (int j = 0; j < cols; ++j) {
            arr[i][j] = "   ";
        }
    }
    return arr;
}

// 释放动态二维数组的工具函数
void deleteDynamicArray(string** arr, int rows) {
    for (int i = 0; i < rows; ++i) {
        delete[] arr[i];
    }
    delete[] arr;
}

int main() {
    const int rows = 7;
    const int cols = 7;
    // 动态创建二维数组
    string** chart = createDynamicArray(rows, cols);
    
    chart[6][5] = "EXA";

    // 打印初始数组
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            cout << '[' << chart[i][j] << ']';
        }
        cout << endl;
    }

    // 实例化类,传递动态数组指针
    Example Exa1(6, 5, chart);
    Exa1.print();

    // 测试交换功能
    chart[2][3] = "SWP";
    Exa1.swapElement(2, 3);
    cout << "\n交换后的数组:" << endl;
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            cout << '[' << chart[i][j] << ']';
        }
        cout << endl;
    }

    // 释放动态内存
    deleteDynamicArray(chart, rows);
    return 0;
}

方案2:适配静态二维数组(仅用于当前测试)

如果要继续使用静态数组,需要修改类的参数类型为数组引用:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

const int SIZE = 7;

class Example {
    int x, y;
    string (&chart)[SIZE][SIZE]; // 引用静态二维数组
public:
    Example(int i, int j, string (&arr)[SIZE][SIZE]);
    void print() const { cout << "索引(" << x << "," << y << ")的值:" << chart[x][y] << endl; }
};

Example::Example(int i, int j, string (&arr)[SIZE][SIZE]) : x(i), y(j), chart(arr) {}

int main() {
    string chart[SIZE][SIZE];

    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            chart[i][j] = "   "; // 直接赋值更简单
        }
    }

    chart[6][5] = "EXA";

    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            cout << '[' << chart[i][j] << ']';
        }
        cout << endl;
    }

    Example Exa1(6, 5, chart); // 直接传递数组名
    Exa1.print();

    return 0;
}

关键说明

  • 动态二维数组传递时,直接传递string**指针即可,类中保存该指针就能直接操作原数组(因为是传引用语义,修改类中chart指向的元素就是修改原数组)
  • 静态二维数组因为类型固定(需要指定大小),只能通过数组引用传递,灵活性较差,不适合后续动态创建的需求
  • 构造函数中不要直接解引用未初始化的指针,必须先让指针指向有效的内存地址

内容的提问来源于stack exchange,提问作者b23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 14:35:22