gpt4 book ai didi

c++ - Windows 上的 Std::getline

转载 作者:行者123 更新时间:2023-11-30 03:49:24 31 4
gpt4 key购买 nike

我有以下代码:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
// your code goes here
stringstream instream("a x b c d x c d");
string line;
bool loop;
loop = getline(instream, line);
return 0;
}

它适用于 gcc,但是当我使用 Visual Studio 2013 进行编译时,我在 loop = getline(instream, line) 处遇到构建错误:

cannot convert from 'std::basic_istream<char, std::char_traits<char> > to 'bool'

我该如何解决这个问题?

最佳答案

隐式 bool 值转换规则 changed from C++03 to C++11 ,并且编译器对这些东西的支持在不同平台之间差异很大。

如果你真的需要这个,我会使用旧的 force-boolean 技巧:

loop = !!getline(instream, line);

如果您直接将 getline 插入 if 条件,而不是首先将其分配给变量,则不需要此技巧,因为 if is special .

关于c++ - Windows 上的 Std::getline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32572685/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com