错误 C2679:二进制'<<':找不到采用类型 'overloaded-function' 的右侧操作数的运算符(或者没有可接受的转换)
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
这是我的原始代码:
#include <iostream>
void printArrayValues(int x, int y)
{
std::cout << x << std::endl;
std::cout << y << std::endl;
}
int main()
{
int myArray[2];
std::cout << "Please enter what you want the first element in 'myArray' to be: ";
std::cin >> myArray[0] >> std::endl;
std::cout << "Please enter what you want the second element in 'myArray' to be: ";
std::cin >> myArray[1] >> std::endl;
printArrayValues(myArray[0], myArray[1]);
return 0;
}
我查了我的错误,它说这是因为我没有#include <string>
。我对此感到困惑,因为没有声明字符串,但我还是继续#include
了它。它似乎已经修复了它,但是当我回来时,同样的错误又回来了。这是我的新代码:
#include <fstream>
#include <iostream>
#include <istream>
#include <string>
void printArrayValues(int x, int y)
{
std::cout << x << std::endl;
std::cout << y << std::endl;
}
int main()
{
int myArray[2];
std::cout << "Please enter what you want the first element in 'myArray' to be: ";
std::cin >> myArray[0] >> std::endl;
std::cout << "Please enter what you want the second element in 'myArray' to be: ";
std::cin >> myArray[1] >> std::endl;
printArrayValues(myArray[0], myArray[1]);
return 0;
}
错误仍然显示,因此仍未修复,我不确定该怎么办。有人可以告诉我如何修复它并解释为什么该特定修复有效吗?谢谢!
std::cin >> myArray[0] >> std::endl;
不正确。std::endl
仅适用于输出流。例:
std::cout << myArray[0] << std::endl;
如果要跳过换行符的所有内容,请使用std::istream::ignore()
1。
std::cin >> myArray[0];
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
但是,在您的情况下,您不需要它,因为默认情况下operator>>
会忽略前导空格(除非您使用std::noskipws
(,包括换行符。您可以简单地使用:
std::cout << "Please enter what you want the first element in 'myArray' to be: ";
std::cin >> myArray[0];
std::cout << "Please enter what you want the second element in 'myArray' to be: ";
std::cin >> myArray[1];
1:如果您决定使用std::cin.ignore()
,请添加
#include <limits>
得到std::numeric_limits
的定义。
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何显式调用运算符<<
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 过载输出<<用于类的运算符,以打印其中的元组
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<:此运算符函数的参数太多
- C++继承运算符<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 为什么字符串流运算符<<擦除原始值
- 关于使用运算符<<为新手提供C++中的模板
- 我已经完成了<<运算符重载,但它'It’不起作用
- 重载运算符<<输出地址而不是数据成员
- 错误:没有匹配'运算符<<"在'std::cout
- 重载运算符<<用于ostream语法
- 当运算符<存在时,为什么要定义 LT?
- log4cxx访问异常,使用<<运算符和宏
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多