为了账号安全,请及时绑定邮箱和手机立即绑定

no matching function for call to)为什么?

no matching function for call to)为什么?

C++
狐的传说 2023-04-02 16:12:33
include <iostream>#include <cstring>using namespace std;class Date{ int year; int month; int day;public: int Getyear() { return year; } int Getmonth() { return month; } int Getday() { return day; } void Setyear() { }};class Product{ char *name; double price; Date deptime; char *factory; bool easy_break; Date valtime; char *color; double num;public: Product(char *n); Product(char *n,double p=0); Product(char *n,double p,Date d); void SetProduct(char *n,double p,Date d,char *f,bool e,Date v,char *c,int number); void output ();};void Product::output (){ cout<<name<<" "<<price<<endl; cout<<deptime.Getyear()<<"-"<<deptime.Getmonth()<<"-"<<deptime.Getday()<<endl; cout<<factory<<" "<<easy_break<<endl; cout<<valtime.Getyear()<<"-"<<valtime.Getmonth()<<"-"<<valtime.Getday()<<endl;}Product::Product(char *n){ name=new char[strlen(n)+1]; strcpy(name,n); cout<<"Constrcting One...."<<endl;}Product::Product(char *n,double p){ name=n; price=p; cout<<"Constrcting Two...."<<endl;}Product::Product(char *n,double p,Date d){ name=n; price=p; Date deptime(d); cout<<"Constrcting Three...."<<endl;}void Product::SetProduct(char *n,double p,Date d,char *f,bool e,Date v,char *c,int number){ name=n; price=p; Date deptime(d); // factory=f; easy_break=e; Date valtime(v); color=c; num=number;}int main(){ char s[10]; cin>>s; Product p1("car"); Product p2("glass",3.00); Product p3("pen",5.00,2009,3,14); p1.SetProduct("car",100000.0,2009,3,14,"nanjing",0,2010,10,14,"red",1.5); p1.output(); p2.output(); p3.output(); return 0;}
查看完整描述

2 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

问题1:
声明:
Product(char *n);
Product(char *n, double p=0);
调用:
Product p1("car");
存在二义性错误,编译器不知道究竟该调用上面两个构造函数中的哪一个。
问题2:
声明:
Product(char *n);
调用:
Product p1("car");
声明是char*而调用传递的却是字符串字面值,不符合C++标准,应将声明改为Product(const char *n);
问题3:

声明:
Product(char *n);
Product(char *n, double p=0);
Product(char *n, double p, Date d);
调用:
Product p3("pen", 5.00, 2009, 3, 14);
Product类没有接受5个参数的构造函数。

查看完整回答
反对 回复 2023-04-05
?
DIEA

TA贡献1820条经验 获得超2个赞

没有找到你想引用的方法,请仔细检查 你所调用的 方法名称 是否与 原方法名称一致,要一模一样,不然的话,就会出现匹配不到的情况。

查看完整回答
反对 回复 2023-04-05
  • 2 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信