Maison  >  Questions et réponses  >  le corps du texte

vs2015编译c++小程序报错,但是过段时间编译又正常是怎么回事?

程序的目的是收集输入的字符串,看有多少符合特定的字符串

#include <algorithm>
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;
int main()
{
    list<string> ls;
    string s;
    while (cin>> s)
        ls.push_back(s);
    cout<< count(s.cbegin(), s.cend(), "a");
}

编译时报错如下

1>------ 已启动生成: 项目: ConsoleApplication1, 配置: Debug Win32 ------
1>  源.cpp
1>e:\vs2015\vc\include\xutility(3186): error C2446: “==”: 没有从“const char *”到“int”的转换
1>  e:\vs2015\vc\include\xutility(3186): note: 没有使该转换得以执行的上下文
1>  e:\vs2015\vc\include\xutility(3197): note: 参见对正在编译的函数 模板 实例化“std::iterator_traits<_InIt>::difference_type std::_Count_np<const char*,char[2]>(_InIt,_InIt,const _Ty (&))”的引用
1>          with
1>          [
1>              _InIt=const std::codecvt<wchar_t,char,_Mbstatet>::_Byte *,
1>              _Ty=char [2]
1>          ]
1>  c:\users\0e\documents\visual studio 2015\projects\consoleapplication1\consoleapplication1\源.cpp(13): note: 参见对正在编译的函数 模板 实例化“int std::count<std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>,char[2]>(_InIt,_InIt,const _Ty (&))”的引用
1>          with
1>          [
1>              _InIt=std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>,
1>              _Ty=char [2]
1>          ]
1>e:\vs2015\vc\include\xutility(3186): error C2040: “==”:“int”与“const char [2]”的间接寻址级别不同
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

这是第一次编译时的情况,后来我在相同项目下写了别的程序,当再次把该程序覆盖之后编译又完全正常,是怎么回事?是项目冗余问题吗?

好吧,我复制了答案中的代码,所以没问题。一楼你改了代码也不说一声,我愣是没看出来

阿神阿神2714 Il y a quelques jours719

répondre à tous(2)je répondrai

  • 怪我咯

    怪我咯2017-04-17 13:01:02

    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <list>
    #include <string>
    using namespace std;
    int main()
    {
        list<string> ls;
        string s;
        while (cin>> s)
            ls.push_back(s);
        cout<< count(ls.begin(), ls.end(), "a");
    }

    主要“cout<< count(s.cbegin(), s.cend(), "a");”错误,s是一个string,其内元素不能与字符串对比。

    répondre
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:01:02

    类型不匹配。
    如果要从list中找应该是:

    cout<< count(ls.begin(), ls.end(), "a");

    如果要从s中找应该是

    cout<< count(s.cbegin(), s.cend(), ‘a’);

    répondre
    0
  • Annulerrépondre