C++ 将char传递给参数为char的函数有什么问题
错误消息:char类型的参数与“const char*”类型的参数不兼容。我想这是最简单的方法C++ 将char传递给参数为char的函数有什么问题,c++,C++,错误消息:char类型的参数与“const char*”类型的参数不兼容。我想这是最简单的方法 char word1; int compute_score(char word); int main(void) { cin >> word1; int score1 = compute_score(word1); } int compute_score(char word) { // this function then will compute the size
char word1;
int compute_score(char word);
int main(void)
{
cin >> word1;
int score1 = compute_score(word1);
}
int compute_score(char word)
{
// this function then will compute the size of word1
}
#包括
std::stringword1;
int计算分数(标准::字符串字);
内部主(空)
{
标准:cin>>word1;
int score1=计算分数(字1);
std::cout strlen of char?@S.M.哎呀,我太专注于compute_score
了,我错过了。向johann道歉strlen
是一个函数,它的指针指向char,而不是char,所以这是一个错误strlen(word)
。在没有更多上下文的情况下,很难知道如何建议修复。无论如何,你应该意识到char
表示单个字符,而不是字符串。在C中,char
表示一个字符(不是单词,不是字符串)是的,似乎是合理的。但是更好的选择是使用<代码> STD::String 。C++的解决方案,使用字符数组是C程序员所能做的。
# include <iostream>
std::string word1;
int compute_score(std::string word);
int main(void)
{
std::cin >> word1;
int score1 = compute_score(word1);
std::cout << score1;
}
int compute_score(std::string word)
{
return word.length();
}