Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Got a Problem with abstract classes in my map Programm



  • Hello guys,

    i got a task from university, in which i should use a given abstract class in order to create and display a map with cities and streets.
    This is the given abstract class:
    /**
    ///
    #ifndef ABSTRACTMAP
    #define ABSTRACTMAP

    #include "city.h"
    #include "street.h"

    class AbstractMap {
    public:
    typedef std::vector<city *> CityList;
    typedef std::vector<Street *> StreetList;

    /// Adds the provided city to this map.
    virtual void addCity(city *) = 0;
    
    /** Adds the provided street to this map. If the cities linked by the street
     *  have not been added to this map before, the street is not added.
     *
     * Return true if the street has beed added.
     */
    virtual bool addStreet(Street *) = 0;
    
    /**
     * @brief Search for a city in this map by given name.
     * @param name
     * @return the city pointer, if city not found NULL
     */
    

    // virtual City* find_city(const QString city_name) const = 0;

    /**
     * @brief Search for streets in this map.
     * @param city where you want the street_list from
     * @return a list of all streets in this map connected to provided city.
     */
    

    // virtual StreetList get_street_list(const City* city) const = 0;

    /**
     * @brief Look for opposite city.
     * @param street
     * @param city
     * @return opposite city of the street. If city has no connection to street returns NULL.
     */
    

    // virtual City * get_opposite_city(const Street* street, const City* city) const = 0;

    /**
     * @brief Calculate the street length.
     * @param street
     * @return length of the street
     */
    

    // virtual double get_length(const Street* street) const = 0;
    };

    #endif // ABSTRACTMAP

    *//
    The next one is also given:

    #ifndef MAPIO_H
    #define MAPIO_H

    #include "abstractmap.h"

    /// This class adds Cities and Streeds to a Map.
    class MapIo
    {
    public:
    /// this method adds Cities and Streets to the provided Map.
    virtual void fillMap(AbstractMap &map) = 0;
    };

    #endif // MAPIO_H

    **This is the class used to add streets and cities to the map.

    Now my Problem**

    void MainWindow::on_pushButton_2_clicked()
    {
    MapIo::fillMap(&AbstractMap );
    }

    How can i give an &AbstractMap object from a class which is abstract so no object can be created from it? How can i solve this? The Parameters are given so i cant change them?

    Would be really nice if some one could help me.
    If you need further information just ask me.
    Regards


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Implement your own subclass of these abstract classes.

    From your code sample, it looks like you are fairly new to C++. You should first study the language basics before going further.


Log in to reply