简体   繁体   English

错误:basic_istream <char> 使用getline时转换为非标量类型cxx11 :: string

[英]Error: basic_istream<char> to non-scalar type cxx11::string while using getline

I just started a c++ class at my school and I am starting to learn the language. 我刚刚在学校开始了c ++课,并且开始学习这种语言。 For one school problem I am trying to skip lines in a text file using getline. 对于一个学校问题,我试图使用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;
}
}

Every time I try to compile that I get the error 每次我尝试编译时都会出现错误

gift1.cpp:22:21: error: conversion from 'std::basic_istream' to non-scalar type 'std::__cxx11::string {aka std::__cxx11::basic_string}' requested gift1.cpp:22:21:错误:请求从“ std :: basic_istream”转换为非标量类型“ std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}”

Is there any simple solution to this? 有什么简单的解决方案吗?

You have so many problems in this code - so instead of giving you a comment - I added comments to your code 您在此代码中有很多问题-因此,我没有给您注释,而是向您的代码添加了注释

#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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:从 &#39;const char&#39; 转换为非标量类型 &#39;std::string&#39; {aka &#39;std::__cxx11::basic_string<char> &#39;} 请求 - error: conversion from ‘const char’ to non-scalar type ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} requested 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}' 获取“错误:无法转换 'std::__cxx11::string* {aka std::__cxx11::basic_string<char> *}' 到 'const char*' 错误,同时将路径作为参数传递</char> - Getting "error: cannot convert 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'const char*' error while passing a path as argument 第 5 行:字符 54:错误:没有匹配的 function 调用 'min(int, std::__cxx11::basic_string<char> ::尺码类型)'</char> - Line 5: Char 54: error: no matching function for call to 'min(int, std::__cxx11::basic_string<char>::size_type)' 错误:无法从“向量”转换“标签” <std::vector<std::__cxx11::basic_string<char> &gt;&gt;' 到 ' 向量<std::__cxx11::basic_string<char> &gt;' </std::__cxx11::basic_string<char></std::vector<std::__cxx11::basic_string<char> - error: could not convert 'tab' from 'vector<std::vector<std::__cxx11::basic_string<char> >>' to 'vector<std::__cxx11::basic_string<char>>' 'operator*' 不匹配(操作数类型为 'const string' {aka 'const std::__cxx11::basic_string<char> '})</char> - no match for 'operator*' (operand type is 'const string' {aka 'const std::__cxx11::basic_string<char>'}) 尝试生成随机字母错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }' 到 'char' 在分配</char> - Trying to generate a random letter error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' in assignment 错误:传递&#39;const字符串{aka const std :: __ cxx11 :: basic_string <char> }&#39;作为&#39;this&#39;参数 - Error: passing ‘const string {aka const std::__cxx11::basic_string<char>}’ as ‘this’ argument 错误:没有匹配的函数调用&#39;std :: map <std::__cxx11::basic_string<char> - error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char> 错误:“operator+=”不匹配(操作数类型为“long double”和“std::__cxx11::basic_string”<char> &#39;) - error: no match for 'operator+=' (operand types are 'long double' and 'std::__cxx11::basic_string<char>')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM