当前位置:网站首页>由错误<note: candidate expects 1 argument, 0 provided>引发的思考
由错误<note: candidate expects 1 argument, 0 provided>引发的思考
2022-06-26 12:32:00 【老油条666】
最近在刷PAT甲级辅导课的时候,遇到了如标题所示的错误。题目链接:1561. PAT 评测 - AcWing题库
引起错误的代码如下:
#include<bits/stdc++.h>
using namespace std;
int N,M;
// const int K = 6;
int n, k, m;
int P[K];
int K = 6;
struct stu
{
string id;
int grade[K];
int total = 0;
int count;
// stu()
// {
// cout << "默认构造函数执行" << endl;
// }
stu(const stu& oth)
{
this->id = oth.id;
memcpy(this->grade, oth.grade, 4*k);
this->total = oth.total;
this->count = oth.count;
cout << "自定义拷贝构造函数执行" << endl;
}
stu(string _id):id(_id)
{
for(int i = 1; i <= k; ++i)
grade[i] = -2;
total = count = 0;
cout << "自定义构造函数执行" << endl;
}
stu& operator = (const stu& oth)
{
this->id = oth.id;
// this->grade = oth.grade;
memcpy(this->grade, oth.grade, 4*k);
this->total = oth.total;
this->count = oth.count;
return *this;
cout << "重载的赋值操作符执行" << endl;
}
void calc()
{
for(int i = 1; i <= k; ++i)
{
total += max(0, grade[i]);
count += grade[i] == P[i];
}
}
bool has_submit()
{
for(int i = 1; i <= k; ++i)
{
if(grade[i] >= 0)
return true;
}
return false;
}
bool operator < (const stu& oth)
{
if(this->total != oth.total)
return this->total > oth.total;
else if(this->count != oth.count)
return this->count > oth.count;
else
return this->id < oth.id;
}
};
int main()
{
unordered_map<string, stu> students;
cin >> n >> k >> m;
for(int i = 1; i <= k; ++i) cin >> P[i];
while(m--)
{
string u_id;
int p_id, grade;
cin >> u_id >> p_id >> grade;
if(students.count(u_id) == 0)
students[u_id] = stu(u_id); //引起出错的地方
students[u_id].grade[p_id] = max(students[u_id].grade[p_id],
grade);
}
vector<stu> res;
for(auto& [fi, se]: students)
{
if(se.has_submit())
{
se.calc();
res.push_back(se);
}
}
sort(res.begin(), res.end());
for(int i = 0, rank=1; i < res.size(); ++i)
{
if(i && res[i].total != res[i-1].total)
rank = i+1;
cout << rank << " " << res[i].id << " " << res[i].total << " ";
for(int j=1; j <= k; ++j)
{
if(res[i].grade[j] == -2)
cout << "-" ;
else
cout << max(res[i].grade[j], 0) ;
if(j < k)
cout << " ";
}
cout << endl;
}
return 0;
}
引起错误的地方:
按理说,在上述代码中我已经满足了C++的三五法则:完成了拷贝构造函数的自定义实现,但还是报错了。最后经过调试发现,出错的地方是因为没有默认构造函数导致的。
如下测试代码就没有问题:
#include <bits/stdc++.h>
using namespace std;
const int K = 6;
const int k = 5;
const int P[1000] = {0};
struct stu
{
string id;
int grade[K];
int total = 0;
int count;
// stu()
// {
// cout << "默认构造函数执行" << endl;
// }
stu(const stu &oth)
{
this->id = oth.id;
memcpy(this->grade, oth.grade, 4 * k);
this->total = oth.total;
this->count = oth.count;
cout << "自定义拷贝构造函数执行" << endl;
}
stu(string _id) : id(_id)
{
for (int i = 1; i <= k; ++i)
grade[i] = -2;
total = count = 0;
cout << "自定义构造函数执行" << endl;
}
stu &operator=(const stu &oth)
{
this->id = oth.id;
// this->grade = oth.grade;
memcpy(this->grade, oth.grade, 4 * k);
this->total = oth.total;
this->count = oth.count;
return *this;
cout << "重载的赋值操作符执行" << endl;
}
void calc()
{
for (int i = 1; i <= k; ++i)
{
total += max(0, grade[i]);
count += grade[i] == P[i];
}
}
bool has_submit()
{
for (int i = 1; i <= k; ++i)
{
if (grade[i] >= 0)
return true;
}
return false;
}
bool operator<(const stu &oth)
{
if (this->total != oth.total)
return this->total > oth.total;
else if (this->count != oth.count)
return this->count > oth.count;
else
return this->id < oth.id;
}
};
int main()
{
stu A("AAA");
stu B(A);
stu C = A;
return 0;
}同样是没有默认构造函数,但是这个测试代码就能正常工作。因此初步推断当一个类对象作为哈希表的值时,该类必要要有默认的无参构造函数。并且经过调试发现:
1、无论是Windows的编译器还是linux的编译器,当一个类对象作为哈希表的值时,用该类的另一个对象对它进行初始化时,该类是先调用默认无参构造函数后再调用重载的赋值操作符,这可能跟哈希表的底层实现有关系。
2、当该类的实例对象不作为哈希表的值的时候,用一个已有的实例去初始化一个新的同类实例,无论是通过stu B(A) 还是通过stu B = A的方式都是直接走的拷贝构造函数方式。
所以在开发过程中即使类重载了多种构造函数,最好还是保留默认构造函数,多人协同开发中指不定别人会拿你的类作为哈希表的值呢。亦或者是给参数都赋默认值,这也是个可行办法。
另外,上述代码是过不了原题的,需要将上述中的拷贝构造函数和重载的复制操作符注释掉才行,原因应该是跟memcpy拷贝grade这一块代码有关系。
边栏推荐
- Vulnerability scanning and reverse osmosis of Internet anti artifact
- Scala problem solving the problem of slow SBT Download
- TP5 thinkphp5 report serialization of'closure'is not allowed
- Scala-day02- variables and data types
- The best CMDB system
- Spark-day03-core programming RDD operator
- 24 database interview questions that must be mastered!
- TP5 thinkphp5 extension package think Mongo operation mongodb time interval range query
- Generate JDE dot train
- 7-1 数的范围
猜你喜欢

Implementing mixins scheme in applet

Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source
The loss of female scientists

2021 q3-q4 investigation report on the use status of kotlin multiplatform

【概率论】条件概率、贝叶斯公式、相关系数、中心极限定理、参数估计、假设检验
![[solved] data duplication or data loss after laravel paginate() paging](/img/68/7bf51bbf893a91bee24f5f7d4a369f.jpg)
[solved] data duplication or data loss after laravel paginate() paging

Build Pikachu shooting range and introduction

nvm安装教程

Scala problem solving the problem of slow SBT Download

The laravel dingo API returns a custom error message
随机推荐
imagecopymerge
Implementing mixins scheme in applet
Thinkphp5 query report: sqlstate[hy093]: invalid parameter number
Laravel subdomain accesses different routing files and different modules
Encapsulate request request of uni app
2021 q3-q4 investigation report on the use status of kotlin multiplatform
【概率论】条件概率、贝叶斯公式、相关系数、中心极限定理、参数估计、假设检验
Spark-day03-core programming RDD operator
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
Laravel+gatewayworker completes the im instant messaging and file transfer functions (Chapter 4: server debugging errors)
Assembly language (7) operation instruction
2022 edition of investment analysis and "fourteenth five year plan" development prospect forecast report of China's switchgear industry
Five trends of member marketing of consumer goods enterprises in the future
PHP laravel+gatewayworker completes im instant messaging and file transfer functions (Chapter 2: explanation of business logic)
Basic principle of MOS tube and important knowledge points of single chip microcomputer
Introduction to the strongest swarm cluster one click deployment + hydrogen bomb level container management tool
Is it safe to open a securities account
Research on the current situation of China's modified engineering plastics market and demand forecast analysis report 2022-2028
Current situation investigation and investment prospect forecast analysis report of China's electrolytic copper market from 2022 to 2028
Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)