当前位置:网站首页>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 .
边栏推荐
- First pass! Baidu AI Cloud Xiling platform has obtained the authoritative certification of digital human ability evaluation from the Institute of information technology
- Typescript
- Common creation and usage of singletons
- 【Spark】. Explanation of several icons of scala file in idea
- 外观模式(Facade)
- Dark horse notes - Common APIs
- What are the common categories of software testing?
- Electron official docs series: Get Started
- OPLG: 新一代云原生可观测最佳实践
- Beifu cx5130 card replacement and transfer of existing authorization files
猜你喜欢

Detailed explanation of C const: definition and use of C constant

Chapter 01_ Installation and use of MySQL under Linux

装饰器(Decorator)

Electron official docs series: Processes in Electron
![[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect](/img/e3/a666ba2f48e8edcc7db80503a6156d.png)
[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect
![Hdu1724[Simpson formula for integral]ellipse](/img/57/fb5098e150b5f3d91a5d0983a336ee.png)
Hdu1724[Simpson formula for integral]ellipse

Arcpy——InsertLayer()函数的使用:掺入图层到地图文档里
Summary of wechat applet test points

10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆

P2393 yyy loves Maths II
随机推荐
Mysql database explanation (6)
System tasks (display / print class) in Verilog - $display, $write, $strobe, $monitor
Processsing mouse interactive learning
Reflect the technical depth (unable to speed up)
C - Common Subsequence
[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect
UVA10341 solve it 二分
Processing random generation line animation
Use the script to crawl the beautiful sentences of the sentence fan website and store them locally (blessed are those who like to excerpt!)
MySQL讲解(二)
Mode pont
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
Electron official docs series: References
倍福PLC实现绝对值编码器原点断电保持---bias的使用
F - Charm Bracelet
Copy multiple Excel files and name them different
Zoomeeper sets ACL permission control (only specific IP access is allowed to enhance security)
[how to connect the network] Chapter 1: the browser generates messages
Common faults of MySQL database - forgetting database password
Common creation and usage of singletons