Использование undelcared идентификатора в цикле switch-case для анализа файлов CSV - PullRequest
0 голосов
/ 10 апреля 2019

Я анализирую разные .csv файлы, чтобы из терминала при запуске приложения программа спрашивала, что пользователь хотел бы проанализировать (например, пользователь может решить проанализировать определенные столбцы или весь файл - выбор предоставляетсявнутри main()).Однако у меня есть use of undeclared identifier внутри цикла switch case. Я не уверен, как позаботиться о main.

У меня есть много файлов для анализа, но я включаю только фрагмент из двухфайлы, которые воспроизводят ошибку, которую я имею.Самая важная часть кода приведена ниже:

#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <stdio.h>
#include <ctime>
#include <cstdio>
#include <chrono>
#include <atomic>

class parseCSVRow_CTL_STATE
{
public:
    std::string const& operator[](std::size_t index) const {
        return m_data_CTL_STATE[index];
    }    

    std::size_t size() const {
        return m_data_CTL_STATE.size();
    }

    void readNextRow_CTL_STATE(std::istream& str) {
        std::string line;
        std::getline(str, line);
        std::stringstream lineStream(line);
        std::string cell;
        m_data_CTL_STATE.clear();
        while(std::getline(lineStream, cell, ',')) {
            m_data_CTL_STATE.push_back(cell);
        }
        if(!lineStream && cell.empty()) {
            m_data_CTL_STATE.push_back("");
        }
    }
    void execute_CTL_STATE_Parsing();

private:
    std::vector<std::string> m_data_CTL_STATE;
};


class parseCSVRow_GPS
{
public:
    std::string const& operator[](std::size_t index) const {
        return m_data_GPS[index];
    }

    std::size_t size() const {
        return m_data_GPS.size();
    }

    void readNextRow_GPS(std::istream& str) {
        std::string line;
        std::getline(str, line);
        std::stringstream lineStream(line);
        std::string cell;
        m_data_GPS.clear();
        while(std::getline(lineStream, cell, ',')) {
            m_data_GPS.push_back(cell);
        }
        if(!lineStream && cell.empty()) {
            m_data_GPS.push_back("");
        }
    }
    void execute_GPS_Parsing();

private:
    std::vector<std::string> m_data_GPS;
};

std::istream& operator>>(std::istream& str, parseCSVRow_CTL_STATE& data) {
    data.readNextRow_CTL_STATE(str);
    return str;
}

std::istream& operator>>(std::istream& str, parseCSVRow_GPS& data) {
    data.readNextRow_GPS(str);
    return str;
}
// Files are analyzed here
void parseCSVRow_CTL_STATE::execute_CTL_STATE_Parsing()
{
    std::ifstream file_CTL_STATE("/home/to/Desktop/float_controller_state_t_CTL_STATE.csv");

    parseCSVRow_CTL_STATE row_CTL_STATE;
    int n;
    std::cout <<"Enter 0  to parse the entire file:: "<<"\n";
    std::cout <<"Enter 1  to search with TimeStamp:: "<<"\n";
    std::cin>>n;
    if(n==0) {
    // Entire file is parsed
    }
    // Parsing according to TimeStamp
    else if(n==1) {
        std::string tm;
        std::cout<<"Enter the TimeStamp:: ";
        // Additional operations
    }
}    

void parseCSVRow_GPS::execute_GPS_Parsing()
{
    std::ifstream file_GPS("/home/to/Desktop/gps_gprmc_t_GPS_GPRMC_DATA.csv");
    parseCSVRow_GPS row_GPS;

    int m;
    std::cout <<"Enter 0 to parse the entire file::      "<<"\n";
    std::cout <<"Enter 1 to search with Latitude::       "<<"\n";
    // Same procedure as above but different headers
}

// Error here on main 
int main()
{
    int i = 2;
    switch (i)
    {
    case 1:
        execute_CTL_STATE_Parsing(); // <-- use of undeclared identifier execute_CTL_STATE_Parsing
        break;

    case 2:
        execute_GPS_Parsing(); // <-- // <-- use of undeclared identifier execute_GPS_Parsing
        break;
    }
}

Кто-нибудь может объяснить, что мне не хватает в main(), который дает эту ошибку?

1 Ответ

1 голос
/ 11 апреля 2019

Вам необходимо создать объект класса для использования методов класса или сделать их статическими

int main()
{
    parseCSVRow_CTL_STATE ctl_parser;
    parseCSVRow_GPS       gps_parser;

    int i = 2;
    switch (i)
    {
    case 1:

        ctl_parser.execute_CTL_STATE_Parsing();
        break;

    case 2:

        gps_parser.execute_GPS_Parsing();
        break;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...