编译报错“no matching function for call to ”和“candidate expects 2 arguments, 0 provi”

Summer丶snow 2020-05-03 10:21:18
编译报错“no matching function for call to ”和“candidate expects 2 arguments, 0 provided|”不知道怎么回事,请大神指教!
#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
string num, name;
public:
student(string s_num, string s_name);
void show();
};
student::student(string s_num, string s_name): num(s_num),name(s_name){}
void student::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
}

class student1: public student
{
private:
int age;
string addr;
student monitor;
public:
student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
student(s_num, s_name);
age = s_age;
addr = s_addr;
monitor = s_monitor;
}
void student1::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
cout << "age:" << age << endl << "addr:" << addr << endl;
}

int main()
{
student1 stud1("10001", "LiMing", 20, "SD", "LiMing");
stud1.show();
return 0;
}


下面是报错:
引用
||=== Build: Debug in c++text1 (compiler: GNU GCC Compiler) ===|
D:\代码\C++\c++text1\main.cpp||In constructor 'student1::student1(std::__cxx11::string, std::__cxx11::string, int, std::__cxx11::string, student)':|
D:\代码\C++\c++text1\main.cpp|28|error: no matching function for call to 'student::student()'|
D:\代码\C++\c++text1\main.cpp|12|note: candidate: student::student(std::__cxx11::string, std::__cxx11::string)|
D:\代码\C++\c++text1\main.cpp|12|note: candidate expects 2 arguments, 0 provided|
D:\代码\C++\c++text1\main.cpp|4|note: candidate: student::student(const student&)|
D:\代码\C++\c++text1\main.cpp|4|note: candidate expects 1 argument, 0 provided|
D:\代码\C++\c++text1\main.cpp|28|error: no matching function for call to 'student::student()'|
D:\代码\C++\c++text1\main.cpp|12|note: candidate: student::student(std::__cxx11::string, std::__cxx11::string)|
D:\代码\C++\c++text1\main.cpp|12|note: candidate expects 2 arguments, 0 provided|
D:\代码\C++\c++text1\main.cpp|4|note: candidate: student::student(const student&)|
D:\代码\C++\c++text1\main.cpp|4|note: candidate expects 1 argument, 0 provided|
D:\代码\C++\c++text1\main.cpp||In function 'int main()':|
D:\代码\C++\c++text1\main.cpp|43|error: no matching function for call to 'student1::student1(const char [6], const char [7], int, const char [3], const char [7])'|
D:\代码\C++\c++text1\main.cpp|28|note: candidate: student1::student1(std::__cxx11::string, std::__cxx11::string, int, std::__cxx11::string, student)|
D:\代码\C++\c++text1\main.cpp|28|note: no known conversion for argument 5 from 'const char [7]' to 'student'|
D:\代码\C++\c++text1\main.cpp|18|note: candidate: student1::student1(const student1&)|
D:\代码\C++\c++text1\main.cpp|18|note: candidate expects 1 argument, 5 provided|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

...全文
5090 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Summer丶snow 2020-05-23
  • 打赏
  • 举报
回复 3
谢谢各位大哥的帮助!总结一下这个问题是出在构造函数上面。 1. 只有构造函数能使用初始化列表 。 2. 调用基类构造函数只能在子类构造函数的初始化列表上做,不能在其构造函数体内做,在构造函数体里调用基类构造函数只是声明了一个匿名的基类对象,跟当前类对象一点关系也没有,也就是说不会修改到当前类对象的成员。 3. 对于成员变量中含有类的情况,初始化这个类成员变量时的输入格式需要注意。
sdghchj 2020-05-22
  • 打赏
  • 举报
回复
调用基类构造函数只能在构造函数的初始化列表上做。 在构造函数体里调用基类构造函数只是声明了一个匿名的基类对象,跟当前类对象一点关系也没有,也就是说不会修改到当前类对象的成员。
双杯献酒 2020-05-22
  • 打赏
  • 举报
回复
: student(s_num, s_name)
这个是初始化. 在构造基类对象的时候用.

student(s_num, s_name);
这个是普通函数调用.
在对象基本构造结束的时候再调用.
Summer丶snow 2020-05-22
  • 打赏
  • 举报
回复
引用 13 楼 双杯献酒 的回复:

student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
    student(s_num, s_name);
改成

student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
: student(s_num, s_name)
{

请问这有什么不一样的吗?
Summer丶snow 2020-05-03
  • 打赏
  • 举报
回复
现在的结果num和name没数据是怎么回事?
Summer丶snow 2020-05-03
  • 打赏
  • 举报
回复
按照楼上大哥的代码,加了一个基类无参构造函数和一个创建对象时的monitor的参数,然后现在运行的结果有问题,现在结果
引用
num: name: age:20 addr:SD Process returned 0 (0x0) execution time : 0.017 s Press any key to continue.
Summer丶snow 2020-05-03
  • 打赏
  • 举报
回复
引用 2 楼 棉猴 的回复:
#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
	string num, name;
public:
	student(string s_num, string s_name);
	student() {};
	void show();
};
student::student(string s_num, string s_name) : num(s_num), name(s_name) {}
void student::show()
{
	cout << "num:" << num << endl << "name:" << name << endl;
}

class student1 : public student
{
private:
	int age;
	string addr;
	student monitor;
public:
	student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
	void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
	student(s_num, s_name);
	age = s_age;
	addr = s_addr;
	monitor = s_monitor;
}
void student1::show()
{
	cout << "num:" << num << endl << "name:" << name << endl;
	cout << "age:" << age << endl << "addr:" << addr << endl;
}
int main()
{
	student1 stud1("10001", "LiMing", 20, "SD", student("LiMing", "10001"));
	stud1.show();
    return 0;
}
VS2015 C++调试通过
加了一个基类无参构造函数,一个monitor初始化,为什么需要加这两个呢?
棉猴 2020-05-03
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
	string num, name;
public:
	student(string s_num, string s_name);
	student() {};
	void show();
};
student::student(string s_num, string s_name) : num(s_num), name(s_name) {}
void student::show()
{
	cout << "num:" << num << endl << "name:" << name << endl;
}

class student1 : public student
{
private:
	int age;
	string addr;
	student monitor;
public:
	student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
	void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
	student(s_num, s_name);
	age = s_age;
	addr = s_addr;
	monitor = s_monitor;
}
void student1::show()
{
	cout << "num:" << num << endl << "name:" << name << endl;
	cout << "age:" << age << endl << "addr:" << addr << endl;
}
int main()
{
	student1 stud1("10001", "LiMing", 20, "SD", student("LiMing", "10001"));
	stud1.show();
    return 0;
}
VS2015 C++调试通过
Summer丶snow 2020-05-03
  • 打赏
  • 举报
回复
顶一顶,请求帮助!
双杯献酒 2020-05-03
  • 打赏
  • 举报
回复

student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
student(s_num, s_name);


改成

student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
: student(s_num, s_name)
{

Summer丶snow 2020-05-03
  • 打赏
  • 举报
回复
引用 9 楼 Summer丶snow 的回复:
1、
#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
    string num, name;
public:
    student(){};
    student(string s_num, string s_name);
    void show();
};
student::student(string s_num, string s_name): num(s_num),name(s_name){}
void student::show()
{
    cout << "num:" << num << endl << "name:" << name << endl;
}

class student1: public student
{
private:
    int age;
    string addr;
    student monitor;
public:
    student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
    void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
    student(s_num, s_name);
    age = s_age;
    addr = s_addr;
    monitor = s_monitor;
}
void student1::show()
{
    cout << "num:" << num << endl << "name:" << name << endl;
    cout << "age:" << age << endl << "addr:" << addr << endl;
}

int main()
{
    student1 stud1("10001", "LiMing", 20, "SD", student("10001", "LiMing"));
    stud1.show();
    return 0;
}
这一份代码为什么输出的时候前两个数据没结果呢? 输出:
引用
num: name: age:20 addr:SD
这个问题有大哥帮忙看一下吗?
真相重于对错 2020-05-03
  • 打赏
  • 举报
回复
至于他那个代码你最好问他。。。。
Summer丶snow 2020-05-03
  • 打赏
  • 举报
回复
引用 8 楼 真相重于对错 的回复:
那个警告是告诉你要用c++11的标准
好的,谢谢大哥!但是为什么上面那个代码输出的时候前两个数据没结果呢?
Summer丶snow 2020-05-03
  • 打赏
  • 举报
回复
1、
#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
    string num, name;
public:
    student(){};
    student(string s_num, string s_name);
    void show();
};
student::student(string s_num, string s_name): num(s_num),name(s_name){}
void student::show()
{
    cout << "num:" << num << endl << "name:" << name << endl;
}

class student1: public student
{
private:
    int age;
    string addr;
    student monitor;
public:
    student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
    void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
    student(s_num, s_name);
    age = s_age;
    addr = s_addr;
    monitor = s_monitor;
}
void student1::show()
{
    cout << "num:" << num << endl << "name:" << name << endl;
    cout << "age:" << age << endl << "addr:" << addr << endl;
}

int main()
{
    student1 stud1("10001", "LiMing", 20, "SD", student("10001", "LiMing"));
    stud1.show();
    return 0;
}
这一份代码为什么输出的时候前两个数据没结果呢? 输出:
引用
num: name: age:20 addr:SD
真相重于对错 2020-05-03
  • 打赏
  • 举报
回复
那个警告是告诉你要用c++11的标准
Summer丶snow 2020-05-03
  • 打赏
  • 举报
回复
引用 6 楼 真相重于对错 的回复:
我想说的是要使用一种语言最好系统的学习一下,不要靠瞎猜。
class student
{
protected:
	string num, name;
public:
	student(string s_num, string s_name);
	void show();
};
student::student(string s_num, string s_name) : num(s_num), name(s_name) {}
void student::show()
{
	cout << "num:" << num << endl << "name:" << name << endl;
}

class student1 : public student
{
private:
	int age;
	string addr;
	student monitor;
public:
	student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
	void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor):student(s_num, s_name),monitor(s_monitor)
{
	
	age = s_age;
	addr = s_addr;
	
}
void student1::show()
{
	cout << "num:" << num << endl << "name:" << name << endl;
	cout << "age:" << age << endl << "addr:" << addr << endl;
}

int main()
{
	student1 stud1("10001", "LiMing", 20, "SD", { "10001", "LiMing" });
	stud1.show();
	return 0;
}
老哥的代码会warning“D:\代码\C++\c++text1\main.cpp|43|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|”,另外想问一下上面那个老哥的代码为什么前两个数据是没有输出的呢?
真相重于对错 2020-05-03
  • 打赏
  • 举报
回复
我想说的是要使用一种语言最好系统的学习一下,不要靠瞎猜。
class student
{
protected:
	string num, name;
public:
	student(string s_num, string s_name);
	void show();
};
student::student(string s_num, string s_name) : num(s_num), name(s_name) {}
void student::show()
{
	cout << "num:" << num << endl << "name:" << name << endl;
}

class student1 : public student
{
private:
	int age;
	string addr;
	student monitor;
public:
	student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
	void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor):student(s_num, s_name),monitor(s_monitor)
{
	
	age = s_age;
	addr = s_addr;
	
}
void student1::show()
{
	cout << "num:" << num << endl << "name:" << name << endl;
	cout << "age:" << age << endl << "addr:" << addr << endl;
}

int main()
{
	student1 stud1("10001", "LiMing", 20, "SD", { "10001", "LiMing" });
	stud1.show();
	return 0;
}

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧