主函数调用 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();
}
txl 构造时候需要先创建phone txll[100];,然而你的默认构造函数竟然是string = 0 这种有明显bug的函数,所以已进入就崩,如同你随便创建个string a = 0一样崩。
phone(const string names = “”, const string numbers = “”) :name(names), number(numbers) {}
1.代码不完全,帮你补充了几处代码,可能是你方法实现没写对,如 重写=,add方法等,剩下的你自己补充全即可,目前我测试可以运行.
2.代码如下
//
// Created by Army_Ma on 2022/4/4.
//
#ifndef LEETCODE_TXL_HPP
#define LEETCODE_TXL_HPP
//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 =(const phone& p){
name = p.name;
number = p.number;
return *this;
};
};
class txl
{
private:
phone txll[100];
int total;
public:
void add(phone& p){
txll[total] = p;
total++;
};
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;
}
};
#endif //LEETCODE_TXL_HPP
发现实现忘了加进去了,底下是具体实现。
而且有时候报图片上的错误有时候报未加载ucrtbased。pdb 去网上下了sdk也不行
我用的是vs2019
#include<iostream>
#include"txl.h"
using namespace std;
void phone::show()const
{
cout << "name:" << name << "number" << number<<endl;
}
phone& phone::operator=(phone&p)
{
name = p.name;
number = p.number;
return *this;
}
void txl::add(phone& p)
{
txll[total] = p;
total++;
}
void txl::show()const
{
for (int i = 1; i < total; i++)
{
txll[i].show();
}
}
bool txl::search(const string& p)const
{
for (int i = 1; i < total; i++)
{
if (txll[i].getname() == p)
return true;
}
return false;
}
bool txl::change(const int& p)
{
if (p > total)
return false;
else
{
string p1;
cout << "new name:" << endl;
cin >> p1;
txll[p].getname() = p1;
cout << "new number:" << endl;
cin >> p1;
txll[p].getnum() = p1;
txll[p].show();
return true;
}
}