与"std::运算符"中的'operator <<'不匹配<<
no match for 'operator <<' in 'std::operator<<
void printAst(int x)
{
for( int i = 0; i < x; i++)
{
cout << "*";
}
cout << " (" << x << ")" << endl;
}
void printHisto(int histo[])
{
//cout.precision(3);
int count = 0;
for(double i = -3.00; i < 3.00; i += 0.25)
{
cout << setprecision(3) << i << " to " << i + 0.25 << ": " << printAst(histo[count]) << endl;
// cout << setw(3) << setfill('0') << i << " to " << i + 0.25 << ": " << histo[count] << endl;
count ++;
}
}
我希望我的输出像这样进行格式化,所以我使用了setPrecision(3),也不起作用。
-3.00至-2.75:(0)
-2.75至-2.50: *(1)
-2.50至-2.25: *(1)
-2.25至-2.00:*(6)
-2.00至-1.75: ** * ** (12)
因此,它像这样的格式化
-3至-2.75:3
-2.75至-2.5:4
-2.5至-2.25:5
-2.25至-2:0
-2至-1.75:0
然而,主要问题是,当我尝试将printast称为Histo [Count]时。这就是导致此错误的原因。printast用于打印asteriks,Histo [Count]提供了要打印的asteriks的量。
cout&lt;&lt;setPrecision(3)&lt;&lt;i&lt;&lt;" to"&lt;&lt;i 0.25&lt;&lt;":"&lt;&lt;printast(histo [count])&lt;&lt;endl;
您似乎对<<
如何在流中工作有误解。
cout << 42
看起来像一个具有两个操作数的操作员表达式,但这确实是对具有两个参数的函数的调用(函数的名称为 operator<<
)。此功能返回对流的引用,该流可以链接。
这样的表达:
cout << 1 << 2;
等效于此:
operator<<( operator<<(cout, 1), 2);
现在,问题在于,函数的参数不能为void
,但这就是printAst
返回的。取而代之的是,您需要返回可以流的东西 - 换句话说,operator<<
已经超载的东西。我建议std::string
:
std::string printAst(int x);
{
std::string s = " (" + std::string(x,'*') + ")";
return s;
}
您可以阅读有关操作员过载的更多信息。
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 模板专用化与派生类不匹配