C++ 有没有办法避免发出叮当声的警告+;(紫红色默认参数)而使用向量浮点?
考虑一下这个计划:C++ 有没有办法避免发出叮当声的警告+;(紫红色默认参数)而使用向量浮点?,c++,compilation,c++17,clang++,C++,Compilation,C++17,Clang++,考虑一下这个计划: #include <cstdlib> #include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; int main() { fstream file; int line_count = 0;
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
fstream file;
int line_count = 0;
int months;
string name_file;
std::cout << "Please input the file name for your report file: ";
std::cin >> name_file;
if (name_file != "load_report.txt")
{
std::cout << "Invalid report file!\n";
exit(1);
}
std::cout << "Please provide the number of months to consolidate: ";
std::cin >> months;
if (months <= 0)
{
std::cout << "You need to have at least one month in order to"
<< " consolidate your sales.\n";
exit(1);
}
std::cout << "\n";
file.open(name_file, ios::in);
if (file.is_open())
{
string line;
while (getline(file, line))
{
line_count++;
}
}
else
{
std::cout << "Could not open file!\n";
exit(1);
}
file.close();
file.open(name_file, ios::in);
vector<float> a(line_count);
int k = 0;
if (file.is_open())
{
for (auto & val : a)
{
file >> val;
}
}
else
{
std::cout << "Could not open file!\n";
exit(1);
}
file.close();
int loops = line_count / months;
for (int i = 0; i < loops; ++i)
{
float sum = 0;
std::cout << "Month ";
for (int j = 0; j < months; ++j)
{
std::cout << i * months + j + 1 << " ";
sum += a[i * months + j];
}
std::cout << "sales: $" << sum << "\n";
}
return 0;
}
有没有一个论点可以让这个警告消失?这是我正在尝试编写的一个程序,它从报告文件加载月度报告,并显示给定月份范围内的合并销售额。在我的目录中,手动创建了一个
load_report.txt
文件,我使用该程序使用fstream
读取该文件的内容makestylecheck
检查内部文档指南。您使用的std::vector
构造函数接受类型为std::allocator
(在std::vector
中命名,类型为typedefallocator\u type
)的第二个参数和默认值std::allocator()
。一个选项是将一个值std::allocator()
作为第二个参数传递,这是一个痛苦的过程,因为大量使用std::vector
将需要类似的更改。不过,我觉得clang tidy太挑剔了,因为很少有std::vector
的用户需要担心显式传递分配器,因为默认值(很好)是一个不错的默认值,所以禁用该警告可能是值得的。看起来你的样式检查脚本需要编辑/改进。@Peter实际上它是用clang++-std=c++17测试的,不叮当作响。@Benson-随便什么。我的评论是在你的主题行说它发出了叮当声的警告时发表的,在你修改之前。不管你用什么样的检查工具,在这种情况下都太挑剔了。@Peter,这是我的错。那么,如果使用分配器
,是否意味着我需要对我的程序进行更多更改?
16369 warnings generated.
/home/bl71/bl71/prob-02/prob-02-main.cpp:64:17: warning: calling a function that uses a default
argument is disallowed [fuchsia-default-arguments-calls]
vector<float> a(line_count);
^
/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:507:29: note: default
parameter was declared here
vector(size_type __n, const allocator_type& __a = allocator_type())
^