【问题标题】:C++ istream_iterator is not a member of stdC++ istream_iterator 不是 std 的成员
【发布时间】:2015-06-07 14:18:52
【问题描述】:

谁能告诉我为什么我在编译时写的下面这段代码一直在抱怨istream_iterator is not a member of std 请你告诉我吗? 谢谢大家

#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
//#include<sstream>

struct field_reader: std::ctype<char> {

    field_reader(): std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table() {
        static std::vector<std::ctype_base::mask>
            rc(table_size, std::ctype_base::mask());

        rc[';'] = std::ctype_base::space;
        return &rc[0];
    }
};


struct Stud{
    double VehicleID;
    double FinancialYear;
    double VehicleType;
    double Manufacturer;
    double ConditionScore;


    friend std::istream &operator>>(std::istream &is, Stud &s) {
        return is >> s.VehicleID >> s.FinancialYear >> s.VehicleType >>      s.Manufacturer >> s.ConditionScore;
    }

    // we'll also add an operator<< to support printing these out:
    friend std::ostream &operator<<(std::ostream &os, Stud const &s) {
        return os << s.VehicleID  << "\t"
                  << s.FinancialYear << "\t"
                  << s.VehicleType    << "\t"
                  << s.Manufacturer   << "\t"
                  << s.ConditionScore;
    }
};

int main(){
// Open the file:
std::ifstream in("VehicleData_cs2v_1.csv");

// Use the ctype facet we defined above to classify `;` as white-space:
in.imbue(std::locale(std::locale(), new field_reader));

// read all the data into the vector:
std::vector<Stud> studs{(std::istream_iterator<Stud>(in)),
 std::istream_iterator<Stud>()};

// show what we read:
for (auto s : studs)
    std::cout << s << "\n";
}

所以,如果您发现问题,请告诉我,因为我目前还不能完全确定,我相信我已经放入了所有必要的包含库

【问题讨论】:

    标签: c++ istream-iterator


    【解决方案1】:

    错误消息可能听起来有点误导,但这是编译器可以说的最好的东西。 std::istream_iterator 是在 &lt;iterator&gt; 头文件中声明的,这就是导致您的问题的原因。

    只需将此添加到您的包含中

    #include <iterator>
    

    【讨论】:

    • 嗨,谢谢它解决了我的问题。但由于某种原因我无法打印。我插入一个 csv 文件,将其读入并想要 cout s。为什么不工作,你能告诉吗?
    • @Lexka 我会说这是因为在您阅读 CSV 文件时,您的 &amp;operator&gt;&gt; 过载不包括逗号 (,)。
    • 您好,您能建议更正一下吗?我正在努力解决这个问题。谢谢大佬
    • 首先,我会尝试读取一个不包含逗号分隔值的文件。然后,在使用 CSV 时,您可以读取一个字段,跳过一个逗号,然后再次读取一个字段,或者创建一个虚拟的 char 变量来读取逗号。
    • @Lexka 提出新问题,而不是用不相关的问题混淆完美的问题。
    猜你喜欢
    • 2010-11-29
    • 2019-07-04
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    相关资源
    最近更新 更多