C++ 为什么 std::getline(cin, Number) 给出“没有匹配的调用函数”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9597963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does std::getline(cin, Number) give a "No matching function for call" error?
提问by Joriek
I wrote a program so the user inputs a number and the program outputs its binary representation.
我写了一个程序,让用户输入一个数字,程序输出它的二进制表示。
I get this error:
我收到此错误:
No matching function for call to `getline(std::istream&, unsigned int&)'
没有匹配的函数调用`getline(std::istream&, unsigned int&)'
How can I solve this?
我该如何解决这个问题?
Also, it outputs:
此外,它输出:
0
0
0
0
...when it should output the right value for the input.
...什么时候应该为输入输出正确的值。
#include <iostream>
using namespace std;
int main()
{
int Number;
cin >> Number;
bool Binary[sizeof(int) * CHAR_BIT];
for (unsigned int i = 0; i < sizeof(int) * CHAR_BIT; i++)
Binary[(sizeof(int) * CHAR_BIT - 1) - i] = Number & (1 << i);
for (unsigned int i = 0; i < sizeof(int); i++)
std::cout << Binary[i] << std::endl;
system("pause");
return 0;
}
回答by Greg Hewgill
The getline()
function takes an istream
and a string
, not an integer. So:
该getline()
函数采用 anistream
和 a string
,而不是整数。所以:
string sNumber;
getline(cin, sNumber);
// now convert sNumber to an unsigned int
回答by Johnsyweb
std::getline()
gets a lineinto a std::string
variable. You'll need to convert it to an unsigned int
yourself. The usual way is to use a std::istringstream
instance like so:
std::getline()
将一行放入std::string
变量中。您需要将其转换为您unsigned int
自己。通常的方法是使用这样的std::istringstream
实例:
#include <sstream>
#include <iostream>
#include <iomanip>
int main ()
{
std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);
unsigned int number = 0U;
if (!(iss >> number))
{
// Handle error
}
else
{ // Put your binary conversion logic in a function!
std::cout << to_binary(number) << std::endl;
}
}
Tip: Indentation can greatly improve the readability and maintainability of your code.
提示:缩进可以大大提高代码的可读性和可维护性。
Putting your binary conversion logic into a function will make it easier to test (think "unit-test") and to reuse, like this:
将您的二进制转换逻辑放入一个函数中将使测试(想想"unit-test")和重用更容易,就像这样:
// I've left this logic untouched other than to indent it and to introduce
// braces. It needs to be fixed. Declare before main().
std::string to_binary(unsigned int Number)
{
std::ostringstream oss;
bool Binary[sizeof(int) * CHAR_BIT];
for (unsigned int i = 0; i < sizeof(int) * CHAR_BIT; i++)
{
Binary[(sizeof(int) * CHAR_BIT - 1) - i] = Number & (1 << i);
}
for(unsigned int i = 0; i < sizeof(int); i++)
{
oss << Binary[i] << std::endl;
}
return oss.str();
}
I've left correcting your binary conversion logic as an exercise as this looks like homework.
我已经把纠正你的二进制转换逻辑作为练习,因为这看起来像家庭作业。
回答by swegi
getline
can only be used to read in a complete line as a string. If you want to read in a number, use operator>>
.
getline
只能用于读取整行作为字符串。如果要读取数字,请使用operator>>
.
Example:
例子:
int number;
std::cin >> number;
回答by Rohit Vipin Mathews
回答by Eduardo
The following are the valid signatures of getline:
以下是 getline 的有效签名:
istream& getline(char*, int, char = '\n');
istream& getline(signed char*, int, char = '\n');
istream& getline(unsigned char*, int, char = '\n');
You are not using any of them
你没有使用它们中的任何一个
回答by Kien Truong
Change getline(cin, Number)
to cin >> Number
更改getline(cin, Number)
为cin >> Number