VS—— warning C4018 && error C2679

 warning C4018: “<”: 有符号/无符号不匹配

出错代码:

 for(int j=0;j<detector.size();j++)

出错原因分析: detector 是一个Vector容器 ,detecot.size() 在容器说明中 被定义为: unsigned int 类型, 而j是int 类型 所以会出现: 有符号/无符号不匹配警告
错误改正: 定义j为unsigned类型后就可以了

即: for(unsigned int j=0;j<detector.size();j++)
或者: for(size_t int j=0;j<detector.size();j++)

error C2679: 二进制“<<”: 没有找到接受“_Ty1”类型的右操作数的运算符(或没有可接受的转换)


很奇怪,以前在其他IDE上这么写都没有问题,不知道为什么在VS写就出现了这个错误,我一开始以为是我的代码的问题。可是怎么想都没有想通。 
甚至,我自己在main函数中又写了一遍之后,还是报一样的错误。

#include <iostream>
using namespace std;
int main(){
    string s;
    cin >> s;
    cout << s;
}

解决方法很简单: 
在代码前面加一句

#include <string>

就好了

猜你喜欢

转载自blog.csdn.net/qq_40818798/article/details/85235626