【问题标题】:LinkedQueue implementation used to store flights用于存储航班的 LinkedQueue 实现
【发布时间】:2019-03-19 12:27:31
【问题描述】:

我正在尝试在 C++ 中实现一个 LinkedQueue 结构来存储一些航班的数据。

所以,首先我必须读取一个 csv 文件,它提供了要存储的数据。 LinkedQueue 必须这样工作:必须使用 Flight 类存储每个 Flight 的属性,然后 LinkedQueue 必须有一个名为 FlightNode 的特定节点来最终存储航班。 我的代码没有编译,因为我无法以正确的方式实现 getNext() 函数。

我在下面给出我的代码以及每个类的实现。如果你们能提出任何建议,那将非常有帮助。

非常感谢!!

这是我的 Flight.h 头类:

class Flight { 
public:
    Flight();
    virtual ~Flight();
    string getID();
    void setID(string new_id);
    string getOrigen();
    void setOrigen(string new_origen);
    string getDesti();
    void setDesti(string new_desti);
    string getHora();
    void setHora(string new_hora);

private:
    string id;
    string origen;
    string desti;
    string hora_sortida;

};

Flight.cpp:

Flight::Flight() {

}

Flight::~Flight() {
}

string Flight::getID(){
    return id;
}

string Flight::getOrigen(){
    return origen;
}

string Flight::getDesti(){
    return desti;
}

string Flight::getHora(){
    return hora_sortida;
}

void Flight::setID(string new_id){
    id = new_id;
}

void Flight::setOrigen(string new_origen){
    origen = new_origen;
}

void Flight::setDesti(string new_desti){
    desti = new_desti;
}

void Flight::setHora(string new_hora){
    hora_sortida = new_hora;
}

FlightNode.h:

class FlightNode {
public:
    FlightNode(Flight& f);
    FlightNode(const FlightNode& orig);
    virtual ~FlightNode();
    FlightNode* getNext();
    void setNext(FlightNode* n);
    Flight& getElement();
private:
    Flight* _element;
    FlightNode* _next;

};

FlightNode.cpp:

FlightNode::FlightNode(Flight& f) {
    this->_element = &f;
    this->_next = nullptr;
}

FlightNode::FlightNode(const FlightNode& orig) {
}

FlightNode::~FlightNode() {
}
FlightNode* FlightNode::getNext(){
    return this->_next;
}

void FlightNode::setNext(FlightNode* n){
    this->_next = n; 
}

Flight& FlightNode::getElement(){
    //Don't know how to implement this one, because I declared _element as a pointer but what I need here is to return a reference. 
}

main.cpp:

    string id;
    string origen;
    string desti;
    string hora;

            fstream fin;
            fin.open("flights.csv", ios::in);

            string line, word;
            string id, origen, desti, hora;

            while (getline(fin, line)) {
                stringstream in(line);
                Flight* new_flight = new Flight;
                for (int i = 0; getline(in, word, ','); ++i) {
                    switch (i) {
                        case 0:
                            new_flight->setID(word);
                            break;
                        case 1:
                            new_flight->setOrigen(word);
                            break;
                        case 2:
                            new_flight->setDesti(word);
                            break;
                        case 3:
                            new_flight->setHora(word);
                            break;
                    }
                }
                cout << "id:" << new_flight->getID() << "  origen:" << new_flight->getOrigen() << "  desti: " << new_flight->getDesti() << endl;
                queue.enqueue(*new_flight);
            }

【问题讨论】:

    标签: c++ pointers data-structures segmentation-fault


    【解决方案1】:

    你可以只返回指针后面的引用:

    return *_element;
    

    但是,如果您打算这样做,请确保在上述取消引用时指针不为空。

    【讨论】:

    • 我已经尝试过了,但是我得到了以下错误:LinkedQueue.cpp:38:39: 错误:绑定'const Flight'到'Flight&'类型的引用丢弃限定符FlightNode* tmp = 新的 FlightNode(f); ^ 在来自 LinkedQueue.h:13:0 的文件中,来自 LinkedQueue.cpp:14:FlightNode.h:22:5:注意:初始化 'FlightNode::FlightNode(Flight&)' FlightNode(Flight& f) 的参数 1;
    • 你是在 const 对象上调用函数吗?
    • 不??‍♀️真的迷路了
    • 嗯,签名说这发生在复制构造函数中,其参数签名为const&amp;。你在里面叫它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    相关资源
    最近更新 更多