Search code examples
c++deque

deque.at No Maching Function


I am trying to deque (a string element) from a deque data structure. But I am getting and error:

error: no matching function for call to ‘std::__cxx11::basic_string::basic_string(__gnu_cxx::__alloc_traitsstd::allocator<std::array<std::__cxx11::basic_string<char, 1> >, std::arraystd::__cxx11::basic_string<char, 1> >::value_type&)’ 26 | string record = (string)records.at(0);

deque<array<string, 1>>     records;
string data("hello this is 1st record");
array<string, 1>        buffer{data};
records.push_back(buffer);

string record = (string)records.at(0); //error is reported at this line
printf("%s\n", record.c_str());

Can someone please give me a hint what I am doing wrongly. As background, I have to cache the last 100 text messages, so I am using deque for this purpose.


Solution

  • It is not quite clear why you are using array as elements. The value returned from at is not a string but an array.

    deque<array<string, 1>>     records;
    string data("hello this is 1st record");
    array<string, 1>        buffer{data};
    records.push_back(buffer);
    
    string record = records.at(0)[0];
                            ^^ get first element in deque
                                  ^^ get first element in array
    

    Do not use c-style casts ((string)...). They are almost always wrong (and when they are not, they should be replaced with a safer C++ cast). If you do not use the array (why? when it only holds a single element?) the code is

    deque<string>     records;
    string data("hello this is 1st record");
    records.push_back(data);
    
    string record = records.at(0);
                            ^^ get first element in deque