Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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++_Debugging - Fatal编程技术网

这个运算符重载代码有什么问题? 这个C++代码有什么不对?

这个运算符重载代码有什么问题? 这个C++代码有什么不对?,c++,debugging,C++,Debugging,它可以编译,但不能运行 #include <iostream> using namespace std; class MyClass { private: int *x; int *y; public: MyClass() { x = new int[1]; *x = 0; y = new int[1]; *y = 0; } ~MyClass() { if(x !=

它可以编译,但不能运行

    #include <iostream>

using namespace std;

class MyClass
{
private:
    int *x;
    int *y;
public: 
    MyClass()
    {
        x = new int[1]; *x = 0;
        y = new int[1]; *y = 0;
    }
    ~MyClass()
    {
        if(x != NULL)
        {
            delete [] x;
            x = NULL;
        }
        if(y != NULL)
        { 
            delete [] y;
            y = NULL;
        }
    }
    MyClass operator=(MyClass & rhs)
    {
        MyClass temp;
        temp.Set(rhs.x[0], rhs.y[0]);
        return temp;
    }
    void Set(int a, int b)
    {
        if(x != NULL)
        {           
            *x = a;
        }
        if(y != NULL)
        {           
            *y = b;
        }
    }
    void Show()
    {
        cout<<x[0]<<y[0]<<endl;
    }
};

int main()
{
    MyClass o1;
    o1.Set(10, 20);
    o1.Show();

    MyClass o2;
    o2 = o1;
    o2.Show();
}
#包括
使用名称空间std;
类MyClass
{
私人:
int*x;
int*y;
公众:
MyClass()
{
x=新整数[1];*x=0;
y=新整数[1];*y=0;
}
~MyClass()
{
如果(x!=NULL)
{
删除[]x;
x=零;
}
如果(y!=NULL)
{ 
删除[]y;
y=NULL;
}
}
MyClass运算符=(MyClass和rhs)
{
我的班级温度;
温度设置(rhs.x[0],rhs.y[0]);
返回温度;
}
无效集(整数a,整数b)
{
如果(x!=NULL)
{           
*x=a;
}
如果(y!=NULL)
{           
*y=b;
}
}
无效显示()
{

cout我能看到的唯一明显的错误是您将
x
y
分配为数组,因此需要使用
delete[]x;
而不是
delete x;
,对于
y
也是如此。断言似乎还表明它在删除过程中抛出,因为断言消息中提到的文件是
dbgdel.cpp
,它正在检查块类型是否有效(这很可能检查要删除的块实际上是数组块还是单个实例)

您也不需要对NULL进行检查;
new
总是成功或抛出异常

更多信息:


EDIT:另外,您需要定义一个复制构造函数。请参阅。

我能看到的唯一明显错误是您将
x
y
分配为数组,因此需要使用
delete[]x;
而不是
delete x;
,对于
y
也是如此。断言似乎还表明它在删除过程中抛出,因为断言消息中提到的文件是
dbgdel.cpp
,它正在检查块类型是否有效(这很可能检查要删除的块实际上是数组块还是单个实例)

您也不需要对NULL进行检查;
new
总是成功或抛出异常

更多信息:


编辑:此外,您需要定义一个复制构造函数。请参阅。

虽然实际崩溃可能是由rlbond的回答中提到的不匹配的数组释放引起的,但还有另一个更微妙的选择:

operator=
返回一个副本(不是常规的引用),它是使用默认的复制构造函数创建的。在本例中,它不被使用,因此创建了一个临时对象。临时对象
x
y
指向与副本相同的内存,因为没有定义复制构造函数。当临时对象被销毁时,它的
x
y
将被删除,随后
o2
被销毁,导致同一内存被释放两次

值得注意的是,这门课仍然有很多地方可能出错,需要进一步的关注和爱护。也值得看看我们如何发现这个错误

这是当我把它推过墙时得到的结果。 请注意,这包括导致崩溃的问题,以及许多其他需要修复的问题

 1  #include <iostream>
 2  
 3  using namespace std;
 4  
 5  class MyClass
 6  {
 7  private:
 8      int *x;
 9      int *y;
10  public: 
11      MyClass()
12      {
13          x = new int[1]; *x = 0;
14          y = new int[1]; *y = 0;
15      }
16      ~MyClass()
17      {
18          if(x != NULL)
19          {
20              delete x;
21              x = NULL;
22          }
23          if(y != NULL)
24          { 
25              delete y;
26              y = NULL;
27          }
28      }
29      MyClass operator=(MyClass & rhs)
        _
30      {
diy.cpp 20警告424:对“新[]”数据的不当解除分配(删除)

25              delete y;
33          return temp;
diy.cpp 25警告424:对“新[]”数据的不当解除分配(删除)

25              delete y;
33          return temp;
diy.cpp 33 Info 1772:赋值运算符“MyClass::operator=(MyClass&)”未返回*此

34      }
diy.cpp 34警告1529:符号“MyClass::operator=(MyClass&)”未首先检查对此的赋值

diy.cpp 34 Info 1764:引用参数“rhs”(第29行)可以声明为const ref

diy.cpp 34警告1539:成员“MyClass::x”(第8行)未由赋值运算符赋值

48          cout<<x[0]<<y[0]<<endl;
diy.cpp 34警告1539:成员“MyClass::y”(第9行)未由赋值运算符赋值

48          cout<<x[0]<<y[0]<<endl;
diy.cpp 49 Info 1762:成员函数“MyClass::Show(void)”可以设置为常量

50  };
51  
52  int main()
53  {
54      MyClass o1;
55      o1.Set(10, 20);
56      o1.Show();
57  
58      MyClass o2;
59      o2 = o1;
60      o2.Show();
61  }
62  

虽然实际崩溃可能是由rlbond的回答中提到的阵列的不匹配释放引起的,但还有另一个更微妙的选择:

operator=
返回一个副本(不是常规的引用),它是使用默认的复制构造函数创建的。在本例中,它不被使用,因此创建了一个临时对象。临时对象
x
y
指向与副本相同的内存,因为没有定义复制构造函数。当临时对象被销毁时,它的
x
y
将被删除,随后
o2
被销毁,导致同一内存被释放两次

值得注意的是,这门课仍然有很多地方可能出错,需要进一步的关注和爱护。也值得看看我们如何发现这个错误

这是当我把它推过墙时得到的结果。 请注意,这包括导致崩溃的问题,以及许多其他需要修复的问题

 1  #include <iostream>
 2  
 3  using namespace std;
 4  
 5  class MyClass
 6  {
 7  private:
 8      int *x;
 9      int *y;
10  public: 
11      MyClass()
12      {
13          x = new int[1]; *x = 0;
14          y = new int[1]; *y = 0;
15      }
16      ~MyClass()
17      {
18          if(x != NULL)
19          {
20              delete x;
21              x = NULL;
22          }
23          if(y != NULL)
24          { 
25              delete y;
26              y = NULL;
27          }
28      }
29      MyClass operator=(MyClass & rhs)
        _
30      {
diy.cpp 20警告424:对“新[]”数据的不当解除分配(删除)

25              delete y;
33          return temp;
diy.cpp 25警告424:对“新[]”数据的不当解除分配(删除)

25              delete y;
33          return temp;
diy.cpp 33 Info 1772:赋值运算符“MyClass::operator=(MyClass&)”未返回*此

34      }
diy.cpp 34警告1529:符号“MyClass::operator=(MyClass&)”未首先检查对此的赋值

diy.cpp 34 Info 1764:引用参数“rhs”(第29行)可以声明为const ref

diy.cpp 34警告1539:成员“MyClass::x”(第8行)未由赋值运算符赋值

48          cout<<x[0]<<y[0]<<endl;
diy.cpp 34警告1539:成员“MyClass::y”(第9行)未由赋值运算符赋值

48          cout<<x[0]<<y[0]<<endl;
diy.cpp 49 Info 1762:成员函数“MyClass::Show(void)”可以设置为常量

50  };
51  
52  int main()
53  {
54      MyClass o1;
55      o1.Set(10, 20);
56      o1.Show();
57  
58      MyClass o2;
59      o2 = o1;
60      o2.Show();
61  }
62  

嘿,这是一个很好的工具!我很惊讶!这是一个新的愿景