当前位置:网站首页>Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
Thinking caused by the error < note: candidate expectations 1 argument, 0 provided >
2022-06-26 13:19:00 【Laoyoutiao 666】
Recently brushing PAT During the class a tutorial , Encountered an error as shown in the title . Topic link :1561. PAT evaluating - AcWing Question bank
The code that causes the error is as follows :
#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 << " The default constructor executes " << 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 << " Custom copy constructor execution " << endl;
}
stu(string _id):id(_id)
{
for(int i = 1; i <= k; ++i)
grade[i] = -2;
total = count = 0;
cout << " Custom constructor execution " << 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 << " Overloaded assignment operators execute " << 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); // Where something went wrong
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;
}
What caused the mistake :
Logically speaking , In the above code, I have met C++ Three five rule of : Completed the custom implementation of the copy constructor , It's still wrong . Finally, after debugging, it is found that , The problem is that there is no default constructor .
The following test code is OK :
#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 << " The default constructor executes " << 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 << " Custom copy constructor execution " << endl;
}
stu(string _id) : id(_id)
{
for (int i = 1; i <= k; ++i)
grade[i] = -2;
total = count = 0;
cout << " Custom constructor execution " << 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 << " Overloaded assignment operators execute " << 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;
}Again, there is no default constructor , But the test code will work . Therefore, it is preliminarily inferred that when a class object is used as the value of the hash table , This class must have a default parameterless constructor . After debugging, it is found that :
1、 Whether it's Windows Your compiler is still linux The compiler , When a class object is used as the value of a hash table , When it is initialized with another object of the class , This class calls the default parameterless constructor first, and then calls the overloaded assignment operator , This may have something to do with the underlying implementation of the hash table .
2、 When the instance object of this class is not used as the value of the hash table , Use an existing instance to initialize a new instance of the same kind , Whether through stu B(A) Or through stu B = A All the methods are direct copy constructor methods .
So in the development process, even if the class overloads multiple constructors , It is better to keep the default constructor , In multi person collaborative development, it is not certain that others will take your class as the value of the hash table . Or assign default values to all parameters , This is also a feasible solution .
in addition , The above code can not pass the original question , You need to comment out the copy constructor and overloaded copy operator in the above , The reason should be with memcpy Copy grade This piece of code has something to do with .
边栏推荐
- Electron official docs series: Best Practices
- mariadb学习笔记
- HDU 5860
- Chapter 10 setting up structured logging (2)
- D - skiing
- Script - crawl the customized storage path of the cartoon and download it to the local
- C language: Exercise 2
- 外观模式(Facade)
- Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
- 2. Introduction to parallel interface, protocol and related chips (8080, 8060)
猜你喜欢

C language: Exercise 2

Chapter 10 setting up structured logging (2)

Chapter 01_ Installation and use of MySQL under Linux

Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units

Oplg: new generation cloud native observable best practices

Electron official docs series: Get Started

Common faults of MySQL database - forgetting database password

mysql讲解(一)

MySQL explanation (II)

桥接模式(Bridge)
随机推荐
Electron official docs series: Testing And Debugging
倍福TwinCAT3 NCI在NC轴界面中的基本配置和测试
Mode pont
MySQL数据库讲解(四)
Electron official docs series: Examples
Electron official docs series: Best Practices
IDC report: the AI cloud market share of Baidu AI Cloud ranks first for six consecutive times
外观模式(Facade)
Electron official docs series: Distribution
IDC报告:百度智能云AI Cloud市场份额连续六次第一
组合模式(Composite )
code force Party Lemonade
Common creation and usage of singletons
J - Wooden Sticks poj 1065
Bridge mode
HDU1724[辛普森公式求积分]Ellipse
倍福PLC基于CX5130实现数据的断电保持
Word document export (using fixed template)
Dark horse notes - Common APIs
map 取值