Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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/4/oop/2.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++_Oop_Overloading_Equals_Operator Keyword - Fatal编程技术网

C++ 过载运算符=

C++ 过载运算符=,c++,oop,overloading,equals,operator-keyword,C++,Oop,Overloading,Equals,Operator Keyword,我想重载运算符= 这是我的密码 #define w 10 #define l 10 class grid { private: char battle_grid[w][l]; [...] public: grid(); [...] void show_grid(); grid& operator=(const grid& ex) { battle_grid[w][l] = ex.battle_grid[w][l];

我想重载运算符=

这是我的密码

#define w 10
#define l 10

class grid
{
private:
    char battle_grid[w][l];
    [...]
public:
    grid();
[...]
    void show_grid();
    grid& operator=(const grid& ex)
    {
        battle_grid[w][l] = ex.battle_grid[w][l];
        return *this;
    }
}

grid::grid()
{
int i,j;
    for(i=0;i<w;i++) {
        for(j=0;j<l;j++) {
            battle_grid[i][j] = '.';
        }
    }
}
它不起作用,我也不知道为什么。网格p2为空

您是否意识到:

battle_grid[w][l] = ex.battle_grid[w][l];

不复制整个双数组

您可以添加
网格
的默认构造函数的定义,以及
w
l
的声明/定义吗?您应该为
l
w
添加更多上下文。这些语句是循环的一部分吗?赋值运算符仅对2D数组的一个单独元素执行赋值(位于位置
w,l
——无效位置的元素)。你需要一个回路,是的。。现在可以了。天啊。。多么愚蠢的错误谢谢“它不工作”不是一个足够的问题描述。的确!我不知道为什么我忘了那件事。
battle_grid[w][l] = ex.battle_grid[w][l];