Проблема при вызове функции с именем function2, имеющей карты в качестве параметров функции - PullRequest
0 голосов
/ 17 февраля 2020

Признаюсь, я буквально очень мало знаю об этом призыве по ссылке. Пожалуйста, научите меня, как это исправить и где я ошибся. это мой код C ++:

#include <iostream>
#include <bits/stdc++.h> 
using namespace std;
template<typename KeyType, typename ValueType> 
std::pair<KeyType,ValueType> get_max( const std::map<KeyType,ValueType>& x ) {
  using pairtype=std::pair<KeyType,ValueType>; 
  return *std::max_element(x.begin(), x.end(), [] (const pairtype & p1, const pairtype & p2) {
        return p1.second < p2.second;
  }); 
}

// function to return max of the max vales of the 4 different maps;
short int maximum(short int a, short int b, short int c, short int d){
int ret;
a = max(a,b);
b = max(a,c);
ret = max(b,d);
return ret;
}





 int function2(std::map<int, int>& temp3_map , std::map<int, int>& temp6_map, std::map<int, int>& temp9_map, std::map<int, int>& temp1_map ){
    auto max1=get_max(temp3_map);
    auto max2=get_max(temp6_map);     
    auto max3=get_max(temp9_map);
    auto max4=get_max(temp1_map);      // getting max values opf each sets
    short int maxx; // maxx will be retuned in the end
    char a;
    short int max_here =  maximum(max1.second,max2.second,max3.second,max4.second); // find the max of all 4 sets

    if (max1.second == max_here){
        //default code here
        maxx= max1.second;    // getting value of the max count of all sets into the max

        temp3_map.at('A') = -1;
        temp3_map.at('B') = -1;
        temp3_map.at('C') = -1;
        temp3_map.at('D') = -1;
        a = max1.first;
        temp6_map.at(a) = -1;
        temp9_map.at(a) = -1;        
        temp1_map.at(a) = -1; // in these three lines set (like every set's A to -1)
    }

    else if(max2.second ==max_here){
        //default code here
        maxx= max2.second;    // getting value of the max count of all sets into the max

        temp6_map.at('A') = -1;
        temp6_map.at('B') = -1;
        temp6_map.at('C') = -1;
        temp6_map.at('D') = -1;
        a = max2.first;
        temp3_map.at(a) = -1;
        temp9_map.at(a) = -1;        
        temp1_map.at(a) = -1;
    }

    else if(max3.second ==max_here){
        //default code here
        maxx= max3.second;    // getting value of the max count of all 4 maps into the max

        temp9_map.at('A') = -1;
        temp9_map.at('B') = -1;
        temp9_map.at('C') = -1;
        temp9_map.at('D') = -1;
        a = max3.first;
        temp3_map.at(a) = -1;
        temp6_map.at(a) = -1;        
        temp1_map.at(a) = -1;
    }

    else {
        maxx= max4.second;    // getting value of the max count of all 4 maps into the max

        temp1_map.at('A') = -1;
        temp1_map.at('B') = -1;
        temp1_map.at('C') = -1;
        temp1_map.at('D') = -1;
        a = max4.first;
        temp3_map.at(a) = -1;
        temp6_map.at(a) = -1;        
        temp9_map.at(a) = -1;
    }

    return maxx;
}



int main() {
    std::map<char, int> temp3_map;
      map<char, int>::iterator it1,it2,it3,it4;
    temp3_map['A'] = 0;
    temp3_map['B'] = 5; temp3_map['C'] = 0; temp3_map['D'] = 0; 


    std::map<char, int> temp6_map;
    temp6_map['A'] = 0;
    temp6_map['B'] = 0; temp6_map['C'] = 6; temp6_map['D'] = 0; 

    std::map<char, int> temp9_map;    
    temp9_map['A'] = 0;
    temp9_map['B'] = 0; temp9_map['C'] = 0; temp9_map['D'] = 12; 

    std::map<char, int> temp1_map;
    temp1_map['A'] = 10;
    temp1_map['B'] = 0; temp1_map['C'] = 0; temp1_map['D'] = 0;
 short int profit[4];
 function2(temp3_map,temp6_map,temp9_map,temp1_map);
    return 0;
}

, и вид ошибки, которую я получаю:

main.cpp: In function ‘int main()’:
main.cpp:113:51: error: invalid initialization of reference of type ‘std::map&’ from expression of type ‘std::map’
  function2(temp3_map,temp6_map,temp9_map,temp1_map);
                                                   ^
main.cpp:25:6: note: in passing argument 1 of ‘int function2(std::map&, std::map&, std::map&, std::map&)’
  int function2(std::map<int, int>& temp3_map , std::map<int, int>& temp6_map, std::map<int, int>& temp9_map, std::map<int, int>& temp1_map ){
      ^~~~~~~~~

, что это за ошибка? я думал об использовании функции передачи по значению, но не вызовет ли это проблемы, что после завершения работы функции мы не получим измененные значения всех карт во время вызова функции, включенные в мои карты?

1 Ответ

0 голосов
/ 17 февраля 2020

Как отмечено в моем комментарии: ваш function2 ожидает карту типа std::map<int, int>. Вы передаете ему карты типа std::map<char, int>.

...