Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将char传递给参数为char的函数有什么问题_C++ - Fatal编程技术网

C++ 将char传递给参数为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类型的参数与“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 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();
}