Я отвечаю на вопрос из книги Beginner C++. Я занимаюсь секцией на карте STL. Вопрос заключается в создании карты с предикатом сортировки, и в качестве ключа и определения (строки) в качестве значения должна быть создана пользовательская структура (wordProperty).C++ const struct to const string error
Я получаю сообщение об ошибке;
Ошибка 1 Ошибка C2664: 'BOOL fPredicate :: оператор() (Const станд :: строка &, Const станд :: строка &) сопз': не удается преобразовать аргумент 1 из 'сопзЬ wordProperty' к «сопзЬ станд :: строка & "C: \ Program Files (x86) \ Microsoft Visual Studio 12.0 \ В.- \ включать \ xutility 521 1 C++ test1
Относится к линии 52, которая в этой части кода:
bool operator < (const wordProperty& item) const
{
return(this->strWord < item.strWord);
}
Программа позволяет пользователю вводить слово, вводить ли это слово из латинского языка и вводить определение, затем печатает дикцию после каждой записи.
Любая помощь с этим была бы весьма признательна.
Благодаря Джон
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
template <typename T>
void DisplayContents(const T& Input)
{
for (auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
cout << iElement->first << " -> " << iElement->second << endl;
cout << endl;
}
struct fPredicate
{
bool operator()(const string& str1, const string& str2) const
{
string str1temp(str1), str2temp(str2);
transform(str1.begin(), str1.end(), str1temp.begin(), tolower);
transform(str2.begin(), str2.end(), str2temp.begin(), tolower);
return(str1temp < str2temp);
}
};
struct wordProperty
{
string strWord;
bool bIsFromLatin;
wordProperty(const string& strWord, const bool & bLatin)
{
this->strWord = strWord;
bIsFromLatin = bLatin;
}
bool operator == (const wordProperty& item) const
{
return(this->strWord == item.strWord);
}
bool operator < (const wordProperty& item) const
{
return(this->strWord < item.strWord);
}
operator const char*() const
{
string temp = this->strWord;
if (this->bIsFromLatin)
{
temp += " is from Latin.";
}
else
temp += " is not from Latin.";
return temp.c_str();
}
};
int main()
{
map<wordProperty, string, fPredicate> mapWordDefinition;
while (true)
{
cout << "Add a new word: ";
string newWord;
cin >> newWord;
cout << "Is this word from Latin (Y/N)? ";
string YN;
bool newLatin;
if ((YN == "Y") || (YN == "YES") || (YN == "y") || (YN == "yes") || (YN == "Yes"))
newLatin = true;
else
newLatin = false;
cout << "What is the definition of this word? ";
string definition;
cin >> definition;
mapWordDefinition.insert(make_pair(wordProperty(newWord, newLatin), definition));
cout << endl;
DisplayContents(mapWordDefinition);
cout << endl;
}
}
эй, спасибо за это. Поэтому я изменил код на это; \t структура fPredicate \t { \t \t оператор BOOL() (Const wordProperty & WP1, Const wordProperty & wp2) Const \t \t { \t \t \t строка str1temp = wp1.strWord; \t \t \t string str2temp = wp2.strWord; \t \t \t return (str1temp
@JonathanCain, я смущен. Вы приняли мой ответ сразу после вашего комментария. Должен ли я предположить, что вы смогли решить вашу проблему? –
Не жалко, я просто подумал, что вы исправили предыдущую ошибку, поэтому я ее пометил, мне не удалось решить проблему с идентификатором ... –