0

I have the following code, where I try to insert values into a multimap of 2 strings, but I keep getting an error that I cannot understand. I've been trying to solve this for hours.

The whole point of the program is to sort the lines of a dictionary based on the automatic sorting of the multimap insertion.

// sort_entries_of_multiple_dictionaries.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <fstream> #include <vector> #include <map> #include <string> #include <algorithm> #include <iomanip> #include <sstream> // Prototypes int indexDict(std::multimap<std::string, std::string>& dict); int main() { std::multimap<std::string, std::string> dict; if(indexDict(dict) == 0) return 0; } int indexDict(std::multimap<std::string, std::string>& dict) { std::ifstream inputFile{ "output.txt", std::ios::in }; std::string currentDictEntry{}; size_t currentLine{}; if (!inputFile) { std::cerr << "input.txt FILE NOT FOUND in the current directory" << std::endl; system("pause"); return 0; } while (std::getline(inputFile, currentDictEntry)) { //std::cout << currentDictEntry << std::endl; // TO DELETE std::string currentWord{}; size_t delimiterPos = currentDictEntry.find('\t', 0); if (delimiterPos == std::string::npos) std::cerr << "ERROR. Delimiter \"<b>\" not found in line " << currentLine << std::endl; else { //std::cout << "pos of \\t = " << delimiterPos << std::endl; // TO DELETE for (char& ch : currentDictEntry) { if (ch != '\t') { currentWord += ch; } else break; } std::cout << currentWord /* << '|' */ << std::endl; // TO DELETE auto value = currentDictEntry.substr(delimiterPos, std::string::npos); std::cout << "size= " << value.size() << '|' << value << std::endl; dict.insert( currentWord, currentWord/*, value*/ ); } if (currentLine == 50) return 0; // TO DELETE currentLine++; } return 1; } if (currentLine == 50) return 0; // TO DELETE currentLine++; } return 1; } 

The error I keep getting is:

unary '++': '_Iter' does not define this operator or a conversion to a type acceptable to the predefined operator illegal indirection 
2
  • Try to replace insert with emplace. The former expects an std::pair, not two separate arguments. Commented Mar 28, 2021 at 14:13
  • It looks like you have unbalanced curly braces, and that the ++ operator is outside the scope of any function. Commented Mar 28, 2021 at 14:17

2 Answers 2

2

as @Evg said, it accepts a std::pair

dict.insert(std::make_pair(currentWord, value)); 

if I understand your intention correctly, you don't want to save the \t into your result, so add 1 after delimiterPos to get the correct value:

auto value = currentDictEntry.substr(delimiterPos + 1, std::string::npos); 

test run. output.txt:

4 d 1 a 2 b 3 c 0 

output:

"0" - "" "1" - "a" "2" - "b" "3" - "c" "4" - "d" 

full code:

#include <iostream> #include <fstream> #include <map> #include <string> #include <iomanip> #include <sstream> // Prototypes int indexDict(std::multimap<std::string, std::string>& dict); int main() { std::multimap<std::string, std::string> dict; if (indexDict(dict) == 0) return 0; for (auto& i : dict) { std::cout << "\"" << i.first << "\" - \"" << i.second << "\"\n"; } } int indexDict(std::multimap<std::string, std::string>& dict) { std::ifstream inputFile{ "output.txt", std::ios::in }; std::string currentDictEntry{}; size_t currentLine{}; if (!inputFile) { std::cerr << "output.txt FILE NOT FOUND in the current directory" << std::endl; system("pause"); return 0; } while (std::getline(inputFile, currentDictEntry)) { //std::cout << currentDictEntry << std::endl; // TO DELETE std::string currentWord{}; size_t delimiterPos = currentDictEntry.find('\t', 0); if (delimiterPos == std::string::npos) std::cerr << "ERROR. Delimiter \"<b>\" not found in line " << currentLine << std::endl; else { //std::cout << "pos of \\t = " << delimiterPos << std::endl; // TO DELETE for (char& ch : currentDictEntry) { if (ch != '\t') { currentWord += ch; } else break; } std::cout << currentWord /* << '|' */ << std::endl; // TO DELETE auto value = currentDictEntry.substr(delimiterPos + 1, std::string::npos); std::cout << "size= " << value.size() << '|' << value << std::endl; dict.insert(std::make_pair(currentWord, value)); } if (currentLine == 50) return 0; // TO DELETE currentLine++; } return 1; } 

small mistakes in your code: you don't need <algorithm> and <vector>. also your error message said input.txt instead of output.txt.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Yes, that was the issue. Regarding the algorithm and vector, I initially used them, but after changing the code they remained as leftovers
2

I Change dict.insert( currentWord, currentWord/*, value*/ ); To dict.insert({ currentWord,currentWord }); and error Has solved

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.