无法在 multiMap C++ 中插入 QCustomPlot::QCPGraph

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

我正在开发一个程序,需要将 QCustomPlot FrameWork 中的 QCPGraph 插入到 std::multimap 中。注意:我对 C++ 还是很陌生。但是,我无法让它工作,这真的很令人沮丧。

这是我的代码:

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)));

最后一行给出了以下编译器错误:

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
                                ^

我知道这可能与指针和类型有关,但我就是想不通。我试着给 &graph(*graph) 插入,这也没有用。请帮忙。

您的容器是:

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

所以它的value_type是:

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

所以当你有:

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

你需要这样插入:

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

或者在 C++11 中:

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

但我认为该图应该归 QCustomPlot 所有,因此您实际上应该存储一个指向它的指针:

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