Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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++ 错误:无法基于转换为类型‘;解析重载函数;int’;_C++ - Fatal编程技术网

C++ 错误:无法基于转换为类型‘;解析重载函数;int’;

C++ 错误:无法基于转换为类型‘;解析重载函数;int’;,c++,C++,我编写了以下代码: #include<bits/stdc++.h> using namespace std; int lengthOfLastWord(string A) { int m= A.size(); int i=0; while(i<m) { if(A[i]==' ') { int count=0; i++; while(A[i]!=

我编写了以下代码:

#include<bits/stdc++.h>
using namespace std;
int lengthOfLastWord(string A) {
    int m= A.size();
    int i=0;
    while(i<m)
    {
        if(A[i]==' ')
        {
            int count=0;
            i++;
            while(A[i]!=' ')
            {
                count++;
                i++;
            }
        }
        else
        i++;
    }
    return count;
}
int main()
{
    string A;
    getline(cin,A);
    cout<<lengthOfLastWord(A);
}
我无法理解为什么它会显示此错误以及我应该如何修复它。 请帮帮我。
谢谢

范围内,如果
范围内,您的
计数
变量与冲突(从技术上讲是“阴影”)。但是,您的本地
count
return
语句的范围内不存在,因此编译器尝试使用在该点上它知道的唯一
count
,即
std::count

这是一个很好的例子,说明了为什么
使用名称空间std
#include
都是坏主意。如果使用了适当的include和名称空间,那么这段代码会给您一个更清晰的编译错误:

error: 'count' was not declared in this scope

一种解决方案是将count变量重命名为其他变量,如
total


编译器正在考虑使用
std::count
函数而不是count变量,count变量是
int
,最直接的原因是
count
变量在
if
范围中定义,在
return
语句的范围中不可用

但是,您看到的错误令人困惑,因为您在代码中使用了
名称空间std
,并且它使程序中的任何地方都可以看到完全不相关的函数
std::count
,包括
return
语句。从编译器的角度来看,您试图返回一个指向
std::count
的指针,但由于此函数是重载模板,编译器不知道您试图返回的是哪一个,因此使用了error的措辞


如果从代码中删除
使用名称空间std
,这本来是不应该有的,那么代码仍将无法编译,但错误消息将更容易理解。

std::count
是发生错误的站点上可用的所有内容。您缺少了变量范围的概念。函数中的if语句中贴有
count
变量。在该if语句之外不可用。特别是在函数的末尾,它不可用,在这里您执行
返回计数。如果希望
count
在那里可用,请将
count
的decreation移到函数顶部。另外,不要使用命名空间std执行
#包括
只会掩盖错误消息。在糟糕的一天,这两条语句将在其他工作程序中导致错误。不要使用它们。这个函数可以编写得简单得多。请阅读。非常感谢大家的帮助!!
error: 'count' was not declared in this scope