在开关盒循环中使用未删除的标识符来解析 CSV 文件

Use of undelcared identifier inside a switch-case loop for parsing CSV files

本文关键字:标识符 文件 CSV 删除 开关 循环      更新时间:2023-10-16

我正在解析不同的.csv文件,以便在我运行应用程序时从终端询问用户想要解析的内容(例如,用户可以决定解析特定列或整个文件 - 选择在main()内给出(。但是我在switch case循环内有一个use of undeclared identifier,我不确定如何在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()中缺少什么导致此错误?

你需要创建类对象来使用类方法或使它们成为静态的

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