error: taking address of temporary [-fpermissive]

Started by
5 comments, last by rip-off 10 years, 7 months ago

Hi

I get on the line: &Flughafen->toString()

the error: taking address of temporary [-fpermissive]

loopAirportpush returns passendeAirports: std::vector<Airport*> passendeAirports;

Any ideas on how to avoid that error?

Many thanks

stringstream bla;
auto foundAirports = loopAirportpush("LSZH");

cout << foundAirports.size() << " airports found: " << endl;

for (auto it = foundAirports.begin(); it != foundAirports.end(); it++)
{
auto Flughafen = (*it);

bla << " - " << &Flughafen->toString() << "\n";
}
return bla.str();

Advertisement

What does the toString() function do and what does it return? Post for us both the definition and implementation of this function.

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 QString toString() const;

...

-------

Thanks

Do you know what the implemented (actually coded) contents of the toString() function are? Did you write this code?

Couple of things:

1. If toString() is supposed to return a string object, then you probably shouldn't be taking the address of said object in order to print it out via an ostream.

2. You're taking the address of a temporary returned from a function. The warning is there because its attempting to tell you that the address is to a temporary object whose lifetime ends in the very near future.

3. I suspect your code is doing what you expect it to do.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

foundAirports is a std::vector<Airport*>

now how to get the name? The code is not mine (gpl) but I suppose that's what toString should be used for?

Thanks


now how to get the name?

Get the name of what exactly? You have a bunch of airports, are you trying to get the name of the first one? All of them?


The code is not mine (gpl) ...

Which code isn't yours? The Airport class, or perhaps QString?


... but I suppose that's what toString should be used for?

It depends on the codebase. Most I've come across treat toString() as a debugging tool, so the content is a convenient programmer's description of the object, but not necessarily suitable for an end user. But it really is down to the project conventions.

For example, an Aiport class might have a toString() that returns a compound string containing the human name and the airport code, e.g. "London Heathrow Airport [LHR]". In a database system, it might also include identifiers. Clients that wish to render a representation of the Airport would use member functions such as name() or getName(), code() or getCode() to build a U.I. suitable for end users. For instance, a HTML based U.I. might place tags around the name or code to emphasise or de-emphasise as necessary.

I think it is clearer to use explicit member functions to render objects, rather than rely on the programmers memory that Airport::toString() returns the name, for example.

This topic is closed to new replies.

Advertisement