简体   繁体   English

为什么我在以下代码段中收到UMR(未初始化的内存读取)错误

[英]Why I am getting UMR(unInitialized Memory Read)Error in following code snippet

Why I am getting UMR(unInitialized Memory Read)Error in following code snippet. 为什么在以下代码段中出现UMR(未初始化内存读取)错误。 I am getting UMR error while calling copy constructor of derived class 调用派生类的副本构造函数时出现UMR错误

class base{
public :

    base(int);
    int id;

    int getid()
    {
        return id;
    }
};

base::base(int a)
{
    id = a;
}

class derived:public base{
public :
    derived(derived &);
    int inum;

    int getnum()
    {
        return inum;
    }

};

derived::derived(derived &d):base(d.getnum()) // UMR Error
{
    inum = id;
}

Can anybody have any idea what is wrong with the implementation? 有人能知道实现有什么问题吗?

You derived class never initialize inum variable. 您派生的类永远不会初始化inum变量。 Error message said it to you. 错误消息告诉您。 d.getnum() returns uninitialized inum variable. d.getnum()返回未初始化的inum变量。

I don't think you can compile your code, a few issues dressed below: 我认为您无法编译代码,以下是一些问题:

class derived:class base{
               ^^^-> public

should be: 应该:

class derived : public base

change int getnum(); 更改int getnum(); to int getnum() const;

derived::derived(derived &d):base(d.getnum) // UMR Error
                                         ^^^ function call? 

why initialize base::id with derived::inum? 为什么用派生:: inum初始化base :: id?

should be: 应该:

   derived::derived(const derived &d) : base(d) // initialize base with base
   {
                    ^^^  copy constructor has const 
      inum = d.inum;  // initialize derived member
      // inum = id;  why you assign id back to inum again?
   }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在以下代码中没有出现错误? - Why I am not getting error in following code? 为什么在尝试使 i = i*i 时出现错误“使用未初始化的 memory 'i'”和“使用未初始化的局部变量 'i'” - Why am I getting the error “using uninitialized memory 'i'” and “uninitialized local variable 'i' used” when trying to make i = i*i 为什么在执行以下代码时出现错误? - Why i am getting error while executing with the following code? 我在以下代码中收到总线错误 - I am getting a bus error in the following code 获取有关使用未初始化 memory 的错误代码 - getting an error code about using uninitialized memory 以下代码段中的错误在哪里,我已经完成了链表的实现,并在链表的末尾添加了元素 - Where is the Error in following code snippet, I have made a linkedlist implementation and I am adding elemnts at tail of the LinkedList 为什么我在以下代码中的每个输出后都得到 32767? - Why am I getting 32767 after each output in the following code? 为什么我得到以下代码 AddressSanitizer: SEGV on unknown address error - why am i getting for the following code AddressSanitizer: SEGV on unknown address error 使用模板重载 &lt;&lt;:为什么会出现以下错误? - Overloading << with templates: Why am I getting the following error? 为什么我在这段代码中遇到运行时错误 - why I am getting runtime error in this code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM