I've written the following as part of a text command parser for a text adventure game.
I'm attempting to associate a string input by a user to an item in an enum class. The following is in my header file:
#include <iostream>
#include <map>
#include <string>
using namespace std;
enum class Noun
{
// Interrogation subjects
name, // ask the subject his name
base, // where is the base?
attack, // when will the attack be?
invalid
};
map < string, Noun > knownNouns;
knownNouns["name"] = Noun::name;
knownNouns["base"] = Noun::base;
knownNouns["attack"] = Noun::attack;
Noun parseNoun(string &noun)
{
auto n = knownNouns.find(noun);
if ( n == knownNouns.end() ) {
return Noun::invalid;
}
return n->second;
When I put this through the compiler, I get the following:
nouns.h:46:1: error: 'knownNouns' does not name a type
knownNouns["name"] = Noun::name;
^
nouns.h:47:1: error: 'knownNouns' does not name a type
knownNouns["base"] = Noun::base;
^
nouns.h:48:1: error: 'knownNouns' does not name a type
knownNouns["attack"] = Noun::attack;
^
nouns.h: In function 'Noun parseNoun(std::string&)':
nouns.h:52:10: error: 'n' does not name a type
auto n = knownNouns.find(noun);
^
nouns.h:53:10: error: 'n' was not declared in this scope
if ( n == knownNouns.end() ) {
^
nouns.h:54:16: error: 'Noun' is not a class or namespace
return Noun::invalid;
^
nouns.h:56:12: error: 'n' was not declared in this scope
return n->second;
^
This is my first time attempting to use maps and enums, and I'm not sure what it is I did wrong. I'm also not terribly familiar with the use of auto vars, so that's a bit of cargo-cult programming on my part. I'm hoping I understood its implementation and the error will clear once I resolve the type definition problem I'm having.
edit: That's embarassing. I copy pasted a mistake that I had already corrected. the code still does not compile when (i.e., the same issue occurs when)
map < string, Noun > knownNouns;
knownNouns["name"] = Verb::name;
knownNouns["base"] = Verb::base;
knownNouns["attack"] = Verb::attack;
is corrected as
map < string, Noun > knownNouns;
knownNouns["name"] = Noun::name;
knownNouns["base"] = Noun::base;
knownNouns["attack"] = Noun::attack;
C++ code. Use CamelCase for all names. Start types (such as classes, structs, and typedefs) with a capital letter, other names (functions, variables) with a lowercase letter.
The "error does not name a type" in C/C++ is defined as the when user declares outside of the function or does not include it properly in the main file this error will through.
You can't place non-declaration constructs directly in namespace scope.
A C++ translation unit is a sequence of declarations.
Non-declaration statements such as assignments must be inside function bodies.
Fixed code:
#include <iostream>
#include <map>
#include <string>
using namespace std;
enum class Noun
{
// Interrogation subjects
name, // ask the subject his name
base, // where is the base?
attack, // when will the attack be?
invalid
};
map< string, Noun > knownNouns =
{
{ "name", Noun::name },
{ "base", Noun::base },
{ "attack", Noun::attack }
};
auto parseNoun( string const& noun )
-> Noun
{
// auto n = knownNouns.find(noun);
// if ( n == knownNouns.end() ) {
// return Noun::invalid;
// }
// return n->second;
return Noun::invalid;
}
Here knownNouns
is initialized. It's not an assignment, even if the =
looks very much like an assignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With