孔.. 2022-04-04 19:28 采纳率: 100%
浏览 48
已结题

构造函数直接报错,但不知道为什么

img

主函数调用 txl 的构造函数时直接报错,构造不了

//txl.h
#pragma once
#include<iostream>
using namespace std;
class phone
{
private:
    string name;
    string number;
    friend class txl;
public:
    string getname() const{ return name; }
    string getnum() const{ return number; }
    void show()const;
    phone(const string names = 0, const string numbers = 0) :name(names), number(numbers) {}
    phone(phone& p) :name(p.name), number(p.number) {}
    phone& operator =(phone&);
};

class txl
{
private:
    phone txll[100];
    int total;
public:
    void add(phone& p);
    void show()const;
    bool search(const string& p)const;
    bool change(const int& p);
    txl(phone& p)
    {
        total = 1;
        txll[total] = p;
        total++;
    }
    txl() 
    {
        total = 1;
    }
};
#include<iostream>
#include"txl.h"
using namespace std;
int main()
{
    
    phone pp("zmas", "123456");
    phone pm=pp;
    txl abc(pp);//就是这里直接报错
    phone pc("szzz", "564565");
    phone pa("ssss", "485665");
    phone pd("znnd", "485645");
    abc.add(pp); abc.add(pc); abc.add(pa); abc.add(pd);
    cout << abc.search("szzz");
    cout<<abc.search("smasmdassdyuas");
    abc.change(3);
    abc.show();

}

  • 写回答

3条回答 默认 最新

  • 关注

    txl 构造时候需要先创建phone txll[100];,然而你的默认构造函数竟然是string = 0 这种有明显bug的函数,所以已进入就崩,如同你随便创建个string a = 0一样崩。

    
        phone(const string names = “”, const string numbers = “”) :name(names), number(numbers) {}
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月12日
  • 已采纳回答 4月4日
  • 创建了问题 4月4日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)