How can I insert in a multimap of maps? - dictionary

How can I insert element in the following map?
map<Longitud,multimap<Latitud, ID> > posicionGeo;
I tried to insert like this:
posicionGeo.insert(make_pair(x.getLongitude(),make_pair(x.getLatitude(),x.getID())));
but this isn't working. Throws this error:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/string:40,
from fecha.h:9,
from principal.cpp:2:
/usr/include/c++/4.8/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int; _U2 = std::pair<int, int>; _T1 = const float; _T2 = std::multimap<float, unsigned int>]’:
CrimeSearch.hxx:33:51: required from here
/usr/include/c++/4.8/bits/stl_pair.h:119:39: error: no matching function for call to ‘std::multimap<float, unsigned int>::multimap(const std::pair<int, int>&)’
: first(__p.first), second(__p.second) { }
^
/usr/include/c++/4.8/bits/stl_pair.h:119:39: note: candidates are:
In file included from /usr/include/c++/4.8/map:62:0,
from CrimeSearch.h:9,
from principal.cpp:4:
/usr/include/c++/4.8/bits/stl_multimap.h:235:9: note: template<class _InputIterator> std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap(_InputIterator, _InputIterator, const _Compare&, const allocator_type&)
multimap(_InputIterator __first, _InputIterator __last,
^
/usr/include/c++/4.8/bits/stl_multimap.h:235:9: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/string:40,
from fecha.h:9,
from principal.cpp:2:
/usr/include/c++/4.8/bits/stl_pair.h:119:39: note: candidate expects 4 arguments, 1 provided
: first(__p.first), second(__p.second) { }
^
In file included from /usr/include/c++/4.8/map:62:0,
from CrimeSearch.h:9,
from principal.cpp:4:
/usr/include/c++/4.8/bits/stl_multimap.h:219:9: note: template<class _InputIterator> std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap(_InputIterator, _InputIterator)
multimap(_InputIterator __first, _InputIterator __last)
^
/usr/include/c++/4.8/bits/stl_multimap.h:219:9: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/string:40,
from fecha.h:9,
from principal.cpp:2:
/usr/include/c++/4.8/bits/stl_pair.h:119:39: note: candidate expects 2 arguments, 1 provided
: first(__p.first), second(__p.second) { }
^
In file included from /usr/include/c++/4.8/map:62:0,
from CrimeSearch.h:9,
from principal.cpp:4:
/usr/include/c++/4.8/bits/stl_multimap.h:177:7: note: std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap(const std::multimap<_Key, _Tp, _Compare, _Alloc>&) [with _Key = float; _Tp = unsigned int; _Compare = std::less<float>; _Alloc = std::allocator<std::pair<const float, unsigned int> >]
multimap(const multimap& __x)
^
/usr/include/c++/4.8/bits/stl_multimap.h:177:7: note: no known conversion for argument 1 from ‘const std::pair<int, int>’ to ‘const std::multimap<float, unsigned int>&’
/usr/include/c++/4.8/bits/stl_multimap.h:166:7: note: std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap(const _Compare&, const allocator_type&) [with _Key = float; _Tp = unsigned int; _Compare = std::less<float>; _Alloc = std::allocator<std::pair<const float, unsigned int> >; std::multimap<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const float, unsigned int> >]
multimap(const _Compare& __comp,
^
/usr/include/c++/4.8/bits/stl_multimap.h:166:7: note: no known conversion for argument 1 from ‘const std::pair<int, int>’ to ‘const std::less<float>&’
/usr/include/c++/4.8/bits/stl_multimap.h:157:7: note: std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap() [with _Key = float; _Tp = unsigned int; _Compare = std::less<float>; _Alloc = std::allocator<std::pair<const float, unsigned int> >]
multimap()
^
/usr/include/c++/4.8/bits/stl_multimap.h:157:7: note: candidate expects 0 arguments, 1 provided

I find the solution, you can do the insert in two steps:
multimap<Latitud, ID> mymap;
mymap.insert(make_pair(x.getLatitude(),x.getID()));
posicionGeo.insert(make_pair(x.getLongitude(),mymap));
but i dont know why this:
posicionGeo.insert(make_pair(x.getLongitude(),make_pair(x.getLatitude(),x.getID())));
dont work, i think that is the same.

Related

Casting a const void* pointer parameter into a const char* pointer, in memchr function

I have to reproduce the functioning of the memchr function, which returns the pointer of the first int c occurence.
Of course, size_t n argument has been measured by func strlen before being sent to ft_memchr func parameter.
But I keep on getting this compiling error (compiled with a main.c) "
ft_memchr.c:10:21: error: operand of type 'const void' where arithmetic or pointer type is required
if ((const char *)s[i] == (const char)c)
"
I'm clearly missing something... It seems I can't cast the const void* parameter into a const char* or even a char*, why is that ?
Thank you.
#include <string.h>
void *ft_memchr(const void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if ((const char *)s[i] == (const char)c)
return ((char *)s + i);
}
return (NULL);
}

X is not a valid template argument for 'const char*' because it is not the address of a variable

I'm trying to use the resources of a temporary class object as a template parameter. But apparently this doesn't work:
godbolt
#include <iostream>
constexpr size_t size(const char* s)
{
int i = 0;
while(*s!=0) {
++i;
++s;
}
return i;
}
template <const char* S>
struct string_struct
{
constexpr string_struct() {
for (int i=0; i < size(S); ++i) {
buf_[i] = S[i];
}
}
constexpr const char* get() {
return buf_;
}
char buf_[size(S)] = {0};
};
constexpr const char some_chars[] = "100";
constexpr auto compose_string()
{
string_struct<some_chars> other_str{};
return string_struct<other_str.get()>;
}
int main()
{
compose_string();
}
gcc 12.1 complains:
<source>: In function 'constexpr auto compose_string()':
<source>:32:41: error: 'other_str.string_struct<(& some_chars)>::get()' is not a valid template argument for 'const char*' because it is not the address of a variable
32 | return string_struct<other_str.get()>;
| ^
<source>:29:16: error: invalid return type 'auto' of 'constexpr' function 'constexpr auto compose_string()'
29 | constexpr auto compose_string()
|
Of course this is a contrived example. What I actually want to do is extend an enhanced version of string_struct recursively. But how can I use its resources for template instantiation? And if that is somehow possible, how do I deduce the return type of a function that returns string_structs (and in the recursive case a string_struct, that is instantiated with another string_struct, that is instantiated with another sstring_struct...)?
Is it even possible?

Is it possible to have a const reference to a nonconst char pointer?

Is it possible to have a const reference to *argv and iterate through the array of pointers argv?
For the code below, I get the following warning:
a reference of type "const char *& (not const-qualified) cannot be initialized with a value of type "char *"
After building, I get this error message:
cannot convert from 'char *' to 'const char *&'.
#import <iostream>
using std::cout;
using std::endl;
int main (int argc, char * argv []) {
for (const char *& c = *argv; *c != '\0'; ++c) {
cout << *c << endl;
}
return 0;
}
I am confused because I know it is possible to have a const reference to a non-const variable like so:
int i = 42;
int &r1 = i;

assignment from incompatible pointer type (struct) c

for the struct
typedef struct Recording_Settings recording_settings;
struct Recording_Settings
{
gchar *profile;
gchar *destination;
};
recording_settings rec_settings;
I get a warning when I try to do this
static void profile_combo_change_cb(GtkComboBox *combo, gpointer userdata)
{
GtkTreeIter iter;
GtkTreeModel *model;
/* Grab the encoding profile choosen */
model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter)) {
gchar *media_type;
gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &media_type, -1);
rec_settings.profile = rb_gst_get_encoding_profile(media_type); // Warning: assignment from incompatible pointer type
g_free (media_type);
}
}
Am I misunderstanding or missing something?
Thanks.
The type of rb_gst_get_encoding_profile seems to be
GstEncodingProfile *rb_gst_get_encoding_profile (const char *media_type);
but you assign its return value to a gchar *.
GstEncodingProfile is a struct type, as far as I can determine (typedef struct _GstEncodingProfile GstEncodingProfile;), and gchar is probably a typedef for a character type (most likely typedef char gchar; from glib). So the types would be incompatible.

serializing union type via custom function: "static assertion failed:: typex::value"

I am serializing the cl_long2 type from OpenCL which is defined like this (simplified from /usr/include/CL/cl_platform.hpp):
typedef int64_t cl_long;
typedef union
{
cl_long __attribute__ ((aligned(16))) s[2];
struct{ cl_long x, y; };
struct{ cl_long s0, s1; };
struct{ cl_long lo, hi; };
} cl_long2;
I defined the following stand-alone serialization function:
namespace boost{ namespace serialization {
template<class Archive> void serialize(Archive &ar, cl_long2 &i, const unsigned version){
ar & make_nvp("x",i.x);
ar & make_nvp("y",i.y);
}
}};
When I compile my code, which invokes a serialization of cl_long2 from as a class member, I am getting the following error, which I cannot understand (the error is the same if it is not a std::vector<cl_long2>, just a stand-alone variable):
/usr/include/boost/archive/detail/check.hpp: In function ‘void boost::archive::detail::check_object_level() [with T = cl_long2]’:
/usr/include/boost/archive/detail/iserializer.hpp:438:9: instantiated from ‘static void boost::archive::detail::load_non_pointer_type<Archive>::invoke(Archive&, T&) [with T = cl_long2, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/detail/iserializer.hpp:592:5: instantiated from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::xml_iarchive, T = cl_long2]’
/usr/include/boost/archive/detail/common_iarchive.hpp:66:9: instantiated from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = cl_long2, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/basic_xml_iarchive.hpp:86:9: instantiated from ‘void boost::archive::basic_xml_iarchive<Archive>::load_override(const boost::serialization::nvp<T>&, int) [with T = cl_long2, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/xml_iarchive.hpp:93:9: instantiated from ‘void boost::archive::xml_iarchive_impl<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<cl_long2>, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:60:9: [ skipping 5 instantiation contexts ]
/usr/include/boost/serialization/split_free.hpp:58:9: instantiated from ‘static void boost::serialization::free_loader<Archive, T>::invoke(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_iarchive, T = std::vector<cl_long2>]’
/usr/include/boost/serialization/split_free.hpp:74:5: instantiated from ‘void boost::serialization::split_free(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_iarchive, T = std::vector<cl_long2>]’
/usr/include/boost/serialization/vector.hpp:151:5: instantiated from ‘void boost::serialization::serialize(Archive&, std::vector<U, Allocator>&, unsigned int) [with Archive = boost::archive::xml_iarchive, U = cl_long2, Allocator = std::allocator<cl_long2>]’
/usr/include/boost/serialization/serialization.hpp:128:9: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_iarchive, T = std::vector<cl_long2>]’
/usr/include/boost/archive/detail/iserializer.hpp:188:5: instantiated from ‘void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::xml_iarchive, T = std::vector<cl_long2>]’
myFile.cpp:368:2: instantiated from here
/usr/include/boost/archive/detail/check.hpp:60:5: error: static assertion failed: "typex::value"
The same error appears regardless of whether the archive type is xml or binary. I am using boost::serialization 1.46.
Any hints?
**EDIT: A smaller example including error is at http://ideone.com/4UgCn
If you look at the line where the assertion fails (detail/check.hpp:60), there is a comment right above it:
// trap attempts to serialize objects marked
// not_serializable
BOOST_STATIC_ASSERT(typex::value);
The documentation of this 'not_serializable' trait says that you have to enable this long2 type first. As you found out, this is done by:
BOOST_CLASS_IMPLEMENTATION(long2,boost::serialization::object_serializable)

Resources