Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        去年关闭。
                                                                                            
                
        
#include <iostream>
using namespace std;
class dummy
{
    private:
    int a,b,*p;
    public:
    void setdata(int x,int y,int z)
    {
        a=x;
        b=y;
        p=&z;
    }
    void showdata()
    {
        cout<<"a "<<a<<"b "<<b<<" pointer address c "<<&p<<endl;
    }
    dummy(dummy &d)
    {
        a=d.a;
        b=d.b;
        p=d.p;
        cout<<"a "<<a<<"b "<<b<<" pointer address c "<<&p<<endl;
    }
    dummy(dummy &d)
    {
        d.a;b=d.b;p=d.p;
    }
};
int main()
{
    dummy d1;//error is here;
    d1.setdata(3,4,5);
    dummy d2=d1;
    d2.showdata();
    d1.showdata();
    return 0;
}


引发错误


  :/ root / copy deep deep / main.cpp | 15 |错误:没有匹配的函数可以调用“ dummy :: dummy()” |


我无法理解为什么会引发错误信息,以及此问题的解决方案是什么

最佳答案

该类没有默认构造函数,因为存在显式定义的副本构造函数

dummy(dummy &d){a=d.a;b=d.b;p=d.p;}


(应使用参数const dummy &声明)

但是在此声明中

dummy d1;


需要缺少的默认构造函数。

您必须明确定义默认构造函数。

考虑到例如该成员函数

void setdata(int x,int y,int z)
{a=x;b=y;p=&z;}


导致不确定的行为,因为退出函数后指针p将具有无效值,因为局部变量(参数)z将被破坏。

09-06 19:34