I just deleted a question that had set for 4 hours unanswered. I have mostly been able to answer it for myself through some trial and error and seem to have a good handle on it except for one piece. Why cant I declare my map as a const or was I doing it wrong? Complete example is at the bottom.
in class header
const std::map <char, char> UppercaseConvert;
in class constructor
const UppercaseConvert = { { 'A','a' },{ 'B','b' },{ 'C','c' },{ 'D','d' },{ 'E','e' },{ 'F','f' },
{ 'G','g' },{ 'H','h' },{ 'I','i' },{ 'J','j' },{ 'K','k' },{ 'L','l' },
{ 'M','m' },{ 'N','n' },{ 'O','o' },{ 'P','p' },{ 'Q','q' },{ 'R','r' },
{ 'S','s' },{ 'T','t' },{ 'U','u' },{ 'V','v' },{ 'W','w' },{ 'X','x' },
{ 'Y','y' },{ 'Z','z' } };
It will compile and work if I remove the const from both declaration and definition so it's not the end of the world. But, since this should be static shouldn't it have the const type?
This is the function it is used in:
std::string BCLogic::ConvertToLowerCase(FString word) {
std::string ConvertedString;
for (char character : word) {
if (UppercaseConvert[character]) {
ConvertedString.push_back(UppercaseConvert[character]);
}
else ConvertedString.push_back(character);
}
return ConvertedString;
}
Edit: Complete Example that will not compile unless you remove const:
#include <iostream>
#include <string>
#include <map>
class Converter {
public:
Converter();
std::string ConvertToLowerCase(std::string);
const std::map <char, char> UppercaseConvert; //remove const to compile
};
Converter::Converter() {
//remove const to compile
const UppercaseConvert = { { 'A','a' },{ 'B','b' },{ 'C','c' },{ 'D','d'},{ 'E','e' },{ 'F','f' },
{ 'G','g' },{ 'H','h' },{ 'I','i' },{ 'J','j' },{ 'K','k' },{ 'L','l' },
{ 'M','m' },{ 'N','n' },{ 'O','o' },{ 'P','p' },{ 'Q','q' },{ 'R','r' },
{ 'S','s' },{ 'T','t' },{ 'U','u' },{ 'V','v' },{ 'W','w' },{ 'X','x' },
{ 'Y','y' },{ 'Z','z' } };
}
std::string Converter::ConvertToLowerCase(std::string word) {
std::string ConvertedString;
for (char character : word) {
if (UppercaseConvert[character]) {
ConvertedString.push_back(UppercaseConvert[character]);
}
else ConvertedString.push_back(character);
}
return ConvertedString;
}
int main() {
Converter ThisConverter;
std::cout << "Enter a word in Caps:";
std::string word;
std::getline(std::cin, word);
word = ThisConverter.ConvertToLowerCase(word);
std::cout << "\n Your converted word is : " << word << std::endl;
return 0;
}