C++ istream_iterator is not a member of std
Asked Answered
B

1

22

Can anyone tell me why the below piece of code I wrote when compiling keeps complaining istream_iterator is not a member of std please can you tell? Thanks guys

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

So please if you spot the issue let me know as I can't quite tell at the moment and I believe I put in all the necessary include libraries

Burch answered 7/6, 2015 at 14:18 Comment(0)
T
38

The error message may sound a bit misleading, but it's the best thing the compiler could say. std::istream_iterator is declared in the <iterator> header file, that's what causes your problem.

Just add this to your includes

#include <iterator>
Technetium answered 7/6, 2015 at 14:20 Comment(10)
Hi, thanks that solved my problem.but I cannot print s for some reason. I insert an csv file, read it in and want to cout s. Why isn't working, can you tell?Burch
@Burch I'd say it's because your &operator>> overload doesn't count with commas (,), while you're reading a CSV file.Technetium
Hello, can you suggest a correction please? I am struggling to resolve this. Thanks manBurch
First of all, I'd try to read a file that doesn't contain commas separating the values. Then, when working with CSV you could read in a field, skip a comma, then read in a field again, or just create a dummy char variable for reading in the commas.Technetium
@Burch ask new questions rather than muddying up perfectly good questions with unrelated issues.Cahn
Lexka you are looking for a chatroom or a mentor to walk you through your issues. This is a Q&A, not a message board.Fulbright
I know, but I am short of time hence why I asked. I just need to resolve this, then make a library and I am doneBurch
@Burch if this post answered your question, accept it (the green mark on the left) and create a new question for your new problem.Technetium
Where's the green button please?Burch
I clicked the green buttonBurch

© 2022 - 2024 — McMap. All rights reserved.