Search code examples
c++qtplotmultimapqcustomplot

Unable to insert QCustomPlot::QCPGraph in multiMap C++


I am working on a program where it is necessary to insert a QCPGraph from the QCustomPlot FrameWork into a std::multimap. Note: I am still pretty new to C++. However, I can't get this to work, which is really frustrating.

Here's my code:

ui->customPlot->addGraph();        

/*
  fill graph with some data
*/

QCPGraph *graph = ui->customPlot->graph(0); 

std::multimap<int, std::pair<std::vector<double>, QCPGraph>, _comparator> myMap; 

//just for demo
std::vector<double> vec; 
vec.at(0) = 2.2; 

myMap.insert(std::make_pair(1, std::make_pair(vec, graph)));

The last line gives me the following compiler errors:

C:\path\mainwindow.cpp:178: Error: no matching function for call to 'std::multimap<int, std::pair<std::vector<double>, QCPGraph>, MainWindow::__comparator>::insert(std::pair<int, std::pair<std::vector<double>, QCPGraph*> >)'
     myMap.insert(std::make_pair(1, std::make_pair(vec, graph)));
                                                                 ^

C:\Qt\Tools\mingw530_32\i686-w64-mingw32\include\c++\bits\stl_multimap.h:524: Error: no type named 'type' in 'struct std::enable_if<false, void>'
       template<typename _Pair, typename = typename
                                ^

I know it probably has to do with the pointers and types, but I just can't figure it out. I tried giving &graph and (*graph) to insert, which didnt work either. Please help.


Solution

  • Your container is:

    std::multimap<int, std::pair<std::vector<double>, QCPGraph>> myMap; 
    

    So its value_type is:

    std::pair<const int, std::pair<std::vector<double>, QCPGraph>>
    

    So when you have a:

    QCPGraph *graph = ui->customPlot->graph(0); 
    

    You need to insert it like this:

    myMap.insert(std::make_pair(1, std::make_pair(vec, *graph)));
    

    Or in C++11:

    myMap.emplace(1, std::make_pair(vec, *graph));
    

    But I think the graph is meant to be owned by QCustomPlot, so you should actually store a pointer to it:

    std::multimap<int, std::pair<std::vector<double>, QCPGraph*>> myMap; 
    myMap.emplace(1, std::make_pair(vec, graph));