开发者

Unable to parse CSV file in C++ QT Error - "No matching function for call to 'getline'."

I am building a program which takes in a .csv file as input and stores all the data in a class. The class has names as columns of the .csv file. Basically, I am using the get line function to get the data from file and copy to string so I can further sort the data for output. It was working in C++ (using visual studio code), but now when I am building it with QT framework, it kind of shows an error -

No matching function for call to getline

The QT part of it is that when the user clicks on the "Load csv", it should load the file and store the data in vector of objects that I created.

csv file

QT window

I know the question is kind of long and my code may not be easily readable as I am new to this, but it would really help to know what is wrong with the code, or how I can make it more simple and readable.

I have attached the class, my function, and also the .csv file.

Thank you in advance.

Class -

#include <iostream>
#include &开发者_如何学JAVAlt;fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

class FlightData
{
private:
    string Time;
    double Latitude;
    double Longitude;
    int Course;
    int KTS;
    int MPH;
    long AltitudeFeet;
    string ReportingFacility;

public:
    FlightData()
    {
        Time = "";
        Latitude = 0;
        Longitude = 0;
        Course = 0;
        KTS = 0;
        MPH = 0;
        AltitudeFeet = 0;
        ReportingFacility = "";
    }

    FlightData(string time, double latitude, double longitude, int course, int kts, int mph, long altitudefeet, string reportfecility)
    {
        Time = time;
        Latitude = latitude;
        Longitude = longitude;
        Course = course;
        KTS = kts;
        MPH = mph;
        AltitudeFeet = altitudefeet;
        ReportingFacility = reportfecility;
    }

    inline string getTime()
    {
        return Time;
    }

    inline double getLatitude()
    {
        return Latitude;
    }

    inline double getLongitude()
    {
        return Longitude;
    }

    int getCourse()
    {
        return Course;
    }

    inline int getKTS()
    {
        return KTS;
    }

    inline int getMPH()
    {
        return MPH;
    }

    inline long getAltitudeFeet()
    {
        return AltitudeFeet;
    }

    inline string getReportingFacility()
    {
        return ReportingFacility;
    }

    inline void setTime(string time)
    {
        Time = time;
    }

    inline void setLatitude(int latitude)
    {
        Latitude = latitude;
    }

    inline void setLongitude(int longitude)
    {
        Longitude = longitude;
    }

    inline void setCourse(int course)
    {
        Course = course;
    }

    inline void setKTS(int kts)
    {
        KTS = kts;
    }

    inline void setMPH(int mph)
    {
        MPH = mph;
    }

    inline void setAltitudeFeet(long altitudefeet)
    {
        AltitudeFeet = altitudefeet;
    }

    inline void setReportingFacility(string reportingfacility)
    {
        ReportingFacility = reportingfacility;
    }

    friend bool operator>(const FlightData& lhs, const FlightData& rhs)
    {
        return lhs.Time > rhs.Time;
    }

    friend bool operator<(const FlightData&  lhs, const FlightData& rhs)
    {
        return lhs.Time < rhs.Time;
    }

    friend bool operator>=(const FlightData& lhs, const FlightData& rhs)
    {
        return lhs.Time >= rhs.Time;
    }

    friend bool operator<=(const FlightData& lhs, const FlightData& rhs)
    {
        return lhs.Time <= rhs.Time;
    }

    friend bool operator!=(const FlightData& lhs, const FlightData& rhs)
    {
        return ((lhs.Time != rhs.Time) || (lhs.Latitude != rhs.Latitude) ||
                (lhs.Longitude != rhs.Longitude) || (lhs.Course != rhs.Course) ||
                (lhs.KTS != rhs.KTS) || (lhs.MPH != rhs.MPH) ||
                (lhs.AltitudeFeet != rhs.AltitudeFeet) ||
                (lhs.ReportingFacility != rhs.ReportingFacility));
    }

    friend bool operator==(const FlightData& lhs, const FlightData& rhs)
    {
        return ((lhs.Time == rhs.Time) && (lhs.Latitude == rhs.Latitude) &&
                (lhs.Longitude == rhs.Longitude) && (lhs.Course == rhs.Course) &&
                (lhs.KTS == rhs.KTS) && (lhs.MPH == rhs.MPH) &&
                (lhs.AltitudeFeet == rhs.AltitudeFeet) &&
                (lhs.ReportingFacility == rhs.ReportingFacility));
    }

    friend ostream& operator<<(ostream& out, const FlightData& object)
    {
        out << object.Time << "  |  " << object.Latitude << "  |  " << object.Longitude
            << "  |  " << object.Course << "  |  " << object.KTS << "  |  "
            << object.MPH << "  |  " << object.AltitudeFeet << "  |  "
            << object.ReportingFacility << endl;

        return out;
    }

    friend istream& operator>>(istream& in, FlightData& object)
    {
        getline(in, object.Time);
        in >> object.Latitude;
        in >> object.Longitude;
        in >> object.Course;
        in >> object.KTS;
        in >> object.MPH;
        in >> object.AltitudeFeet;
        getline(in, object.ReportingFacility);

        in.clear();
        in.ignore(100, '\n');

        return in;
    }
};

QT mainwindow.cpp -

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include "FlightData.h"

std::vector<FlightData> FlightList;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_Load_CSV_clicked()
{
    QString file_name = QFileDialog::getOpenFileName(this, "Open File", ":/resources/Resource files");
    QFile file(file_name);

    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::warning(this, "warning", "File not open!");
    }

    QTextStream in(&file);
    QString text = in.readAll();

    string line = "";

    std::getline(file, line);
    line = "";

    while (std::getline(file, line))
    {
        string time;
        double latitude;
        double longitude;
        int course;
        int kts;
        int mph;
        int altitudefeet;
        string reportingfacility;
        string tempString = "";

        stringstream inputString (line);

        getline(inputString, time, ',');

        getline(inputString, tempString, ',');
        latitude = atof(tempString.c_str());

        tempString = "";
        getline(inputString, tempString, ',');
        longitude = atof(tempString.c_str());

        tempString = "";
        getline(inputString, tempString, ',');
        course = atoi(tempString.c_str());

        tempString = "";
        getline(inputString, tempString, ',');
        kts = atof(tempString.c_str());

        tempString = "";
        getline(inputString, tempString, ',');
        mph = atof(tempString.c_str());

        tempString = "";
        getline(inputString, tempString, ',');
        altitudefeet = atof(tempString.c_str());

        getline(inputString, reportingfacility, ',');

        line = "";

        FlightData newFlightList(time, latitude, longitude, course, kts, mph, altitudefeet, reportingfacility);

        FlightList.push_back(newFlightList);
    }

    ui->plainTextEdit->setPlainText(text);
    file.close();
}

void PrintVector(vector<FlightData>& newflightlist)
{
    sort(newflightlist.begin(), newflightlist.end());

    vector<FlightData>::iterator itr;

    for (itr = newflightlist.begin(); itr != newflightlist.end(); itr++)
    {
        cout << *itr << endl;
    }
}

void MainWindow::on_Sort_Data_clicked()
{
    PrintVector(FlightList);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜