• Advertisement
Sign in to follow this  

how to call function and use return value

This topic is 1648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

how to call and use the return value of:

----

void SDatabase::getNdb(const std::string &id, StdWaypointList *list)
{
    int cnt = m_ndb_map.count(id);
    if (cnt != 0) {
        NdbMapIterator it = m_ndb_map.find(id);
        int i;
        for (i = 0; i < cnt; i++) {
            Waypoint *wpt = (*it).second->asWaypoint();
            list->push_back(wpt);
            it++;
        }
    }
};

-----

If I try like:

           StdWaypointList *testlist;
           it4->second = SDatabase::getNdb(msg,testlist);

I get:

void value not ignored as it ought to be

Many thanks

 

Share this post


Link to post
Share on other sites
Advertisement

yes I see void but how should it be used then? Rewrite returning StdWaypointList? How to call it?

Share this post


Link to post
Share on other sites


yes I see void but how should it be used then? Rewrite returning StdWaypointList? How to call it?

 

Howsabout

SDatabase::getNdb(msg, &it4->second);

Share this post


Link to post
Share on other sites

I have no idea what that function is, but if it takes two parameters and returns nothing, then that is apparently how it should be used. What do you expect it to return since you assign its apparent return value to something?

Share this post


Link to post
Share on other sites

Seems like the possibly undocumented/badly named method needs to return more than 1 thing and thats why those are put into the list it takes as second argument, but OP didnt look closely enough to notice.

           StdWaypointList *testlist;
           SDatabase::getNdb(msg,testlist);
           // now check whats inside the testlist and use it

Share this post


Link to post
Share on other sites

 



yes I see void but how should it be used then? Rewrite returning StdWaypointList? How to call it?

 

Howsabout

SDatabase::getNdb(msg, &it4->second);

 

That looks good to me. Whatever it4 is (I think it's some kind of map/multimap from other posts by OP).

 

The other methods, passing an uninitialised pointer to an StdWaypointList, is just going to crash as soon as it tries to push_back to the list, since testlist doesn't hold the address of a constructed StdWaypointList.

Share this post


Link to post
Share on other sites




Howsabout

SDatabase::getNdb(msg, &it4->second);

 

that returns:

Multiple markers at this line
    - Invalid arguments ' Candidates are: void getNdb(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,
     std::vector<Waypoint *,std::allocator<Waypoint *>> *) '
    - no matching function for call to ‘SDatabase::getNdb(const char*&, Ndb**)’
 

the declaration:

typedef vector<Waypoint*> StdWaypointList;

 

Thanks for ideas.

Share this post


Link to post
Share on other sites

What type is it4->second?

 

when you do "&it4->second", it4->second should be a vector, not a pointer to a vector. If it is a pointer to a vector already, remove the "&".

 

Also, with the code you were first trying to do it with, the StdWaypointlist was not allocated, however the functions expects a pointer to a initialized StdWaypointList. So you would need to allocate a vector first. But thats probably what you werent supposed to do anyways.

Share this post


Link to post
Share on other sites

Yes, but how? I only see in another function:

 

   Waypoint wpt = parseWaypoint(subElem, Waypoint::FLAG_SID);

 

Cannot find any StdWaypointList. Probably that's the function doing? Does it need a return value to increment the list?

 

As for it4 I do before calling that getNdb:

      for (multimap<string, Ndb*>::iterator it4 = m_ndb_map.begin() ; it4 != m_ndb_map.end(); ++it4)
       {

 

Thanks again

Share this post


Link to post
Share on other sites

That function expects a pointer to an existing object of class StdWaypointlist, and it will populate it.

StdWaypointlist waypoints;
SDatabase::getNdb(msg, &waypoints);
 
// Now waypoints contains the result
Edited by Álvaro

Share this post


Link to post
Share on other sites

Thanks now:

 

           Airport* airportliste = SDatabase::getAirport(msg);
           string str2 = &airportliste->toString().toStdString();

 

I get:

error: taking address of temporary [-fpermissive]
splay.cpp:242:58: error: conversion from ‘std::string* {aka std::basic_string<char>*}’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested

 

Airport is declared as:

 

class Airport : public Waypoint
{
public:

    Airport();

    Airport(const QString& id, const QString& name,
            const double& lat, const double& lon, int elevation_ft);

    virtual ~Airport() {};

    virtual Waypoint* deepCopy() const { return new Airport(*this); }

    inline virtual const Airport* asAirport() const { return this; }
    inline virtual Airport* asAirport() { return this; }

    virtual QString toString() const;

    inline const int& elevationFt() const { return m_elevation_ft; }
    int elevationRWYFt() const { return m_runway_map.begin().value().thresholdElevationFt(); }

    inline void addRunway(const Runway& runway_to_add)
    { m_runway_map.insert(runway_to_add.id(), runway_to_add); };

    inline Runway runway(const QString& id) const
    {
        if (!m_runway_map.contains(id)) return Runway();
        return m_runway_map[id];
    }

    inline const RunwayMap& runwayMap() const { return m_runway_map; }
    inline const RunwayMapIterator runwayMapIterator() const { return RunwayMapIterator(m_runway_map); }
    inline RunwayMap& runwayMap() { return m_runway_map; }
    inline uint runwayCount() const { return m_runway_map.count(); }
    inline void clearRunways() { m_runway_map.clear(); }
    inline void setRunwayMap(RunwayMap val) { m_runway_map = val; }

    inline bool hasActiveRunway() const { return !m_active_runway_id.isEmpty(); }
    inline const QString& activeRunwayId() const { return m_active_runway_id; }
    inline Runway activeRunway() const { return runway(m_active_runway_id); }
    void setActiveRunwayId(const QString& id);
protected:

    int m_elevation_ft;
    RunwayMap m_runway_map;
    QString m_active_runway_id;
    Waypoint m_default_airport_location;
};

 

hence I could call airport.asAirport? What is deepcopy for?

Many thanks

Edited by FGFS

Share this post


Link to post
Share on other sites
Where in that code is 242:58 (line 242, column 58)?

EDIT: Oh, it must be this one:
 string str2 = &airportliste->toString().toStdString();
Why the `&'? toStdString returns an std::string, so there's no need to take its address.

To first order, I don't use pointers at all in my code. I use standard containers of objects, not pointers. The only situations where I normally use a pointer are those where I need polymorphic behavior (pointer to base class to call virtual functions on it), and in that case I use unique_ptr.

Can you try to justify every use of a pointer in your code? Edited by Álvaro

Share this post


Link to post
Share on other sites

           Airport* airportliste = SDatabase::getAirport(msg);
           string str2 = &airportliste->toString().toStdString();

 

I get:

error: taking address of temporary [-fpermissive]
splay.cpp:242:58: error: conversion from ‘std::string* {aka std::basic_string<char>*}’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested

 

 

 

 

Really, you can not always ask on a forum when you get a compiler error message, thats just wasting time. You should just learn to read them and try to understand them.

If it says its an error to take the address its basic logic to just not do it and when you get a message about incompatible types you just look at both and correct it to make them compatible.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement