错误:<char>使用 getline 时basic_istream为非标量类型 cxx11::string
Error: basic_istream<char> to non-scalar type cxx11::string while using getline
我刚刚在我的学校开始了C 课程,我开始学习语言。对于一个学校问题,我试图使用GetLine在文本文件中跳过行。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int np;
ifstream fin("gift1.txt.c_str()");
string s;
fin >> np;
string people[np];
for(int counter = 0; counter == 1 + np; ++counter)
{
string z = getline(fin, s)
cout << z << endl;
}
}
每次我尝试收到错误
时礼品1.cpp:22:21:错误:从'std :: basic_istream'转换为non-Scalar type'std :: __ __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}'请求
有什么简单的解决方案?
您在此代码中有很多问题 - 因此,我没有给您评论 - 我在您的代码中添加了评论
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int np;
ifstream fin("gift1.txt.c_str()");
string s; // declare this inside the loop
fin >> np;
string people[np]; // This is a Variable Length array. It is not supported by C++
// use std::vector instead
for(int counter = 0; counter == 1 + np; ++counter)
// End condition is never true, so you never enter the loop.
// It should be counter < np
{
string z = getline(fin, s) // Missing ; and getline return iostream
// see next line for proper syntax
//if(getline(fin, s))
cout << z << endl; // Result will be in the s var.
// Discard the z var completely
}
}
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 为什么istream不支持右值提取
- 将无符号char*转换为std::istream*C++
- 需要从 istream 和 ostream 派生 iostream
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 使用std::istream::peek()总是安全的吗
- 使用 istream 参数读取的 istream 函数
- istream std::cin如何修改自定义istream缓冲区
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 在清除 istream 之前,我不应该需要取消获取它吗?
- C++,如何根据运行时条件构造引用不同 istream 对象的对象?
- 特征中的自定义标量类型
- 如何将QByteArray转换为std::istream或std::ifstream?
- 逗号在重载 ostream 和 istream 在结构中分开
- 为什么从具有较大阵列的 SIMD 内部函数中获得的相对加速比标量更大?
- 为什么 Clang std::ostream 写一个 std::istream 无法读取的双精度?
- 如何在C++中将一个特征张量乘以另一个特征张量的标量和?
- 标量类型的特征模板无法编译固定大小的子矩阵操作
- 结构 init:字符**类型的标量初始值设定项两边的大括号