Question

Here is what i want to do. I want to store de data from a Http response, headers and data. I figured an easy way to do this would be to store the response and the data as a pair. The data is fetched from a LRU-cache. The LRU cache takes a key(string) and the pair. The HTTPResponse is in the form of a POCO C++ HTTPResponse object. But i cant get the string from the second argument of the pair!

        this->clientCache = new LRUPersistentCache<string, pair<HTTPResponse, string > >(3,cachePath);



    pair<HTTPResponse,string> tmp = (*this->clientCache->get(headKey));// Get the pair
    cout << ((string*)tmp.second()).c_str();  //Should get the second object of the pair!
// But gives: Type std::basic_string<char> does not provide a call operator.

Writing it like below gives the same error:

            cout << (*this->clientCache->get(headKey)).second().c_str();

What am I doing wrong here?

Was it helpful?

Solution

cout << ((string*)tmp.second()).c_str(); 
                ^^

you are casting to a string*. It should be just string (or nothing at all) because the second of pair<HTTPResponse,string> is just a string.

and second is a just member not a member function so it should be tmp.second


cout << tmp.second.c_str(); 

OTHER TIPS

second is a memeber value not function defined in standard as

20.3.2 Class template pair

namespace std {
template
struct pair {
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;

thus correct usage of second member value is second not second(). Unless it is functor you are going to use, which is not your case.

To access the elements of a pair, you need:

pair_object.first
// or
pair_object.second

Those are plain member variables, not accessor functions. See http://en.cppreference.com/w/cpp/utility/pair

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top