Unordered Maps on g++ - c++

I am learning about STL currently and I am trying to implement an unordered map for a dictionary file.
This is the first time I have done this so I did a lot of research prior to trying this.
I want to do an unordered map for my assignment because we can receive extra points if we can make our project faster than what our professor's solution currently is.
The problem I am running into is that I keep getting this error:
SpellCheck.h:16: error: ISO C++ forbids declaration of âunordered_mapâ with no type
I am sure my syntax is correct, but I could be missing something.
I am not sure if this helps, but I am compiling on a school server using g++.
My g++ version is g++ (GCC) 4.4.7 .
#ifndef SPELLCHECK_H
#define SPELLCHECK_H
#include <vector>
#include <tr1/unordered_map>
#include <string>
using std::vector;
using std::string;
class SpellCheck
{
private:
typedef vector<string> Vector;
typedef unordered_map<string, int> Dictionary;
};
#endif

This should also work. Compile with -std=c++0x flag to use c++11 with g++.
#ifndef SPELLCHECK_H
#define SPELLCHECK_H
#include <vector>
#include <unordered_map>
#include <string>
class SpellCheck
{
private:
typedef std::vector<std::string> Vector;
typedef std::unordered_map<std::string, int> Dictionary;
};
#endif

Related

Using std::vector does not work

Why the following code doesn't compile,
#include <vector>
using std::vector;
vector<int> v; // Error: too few template arguments, expected 2
but the same code with map (and pair, set, ...) instead of vector works?
#include <map>
using std::map;
map<int, int> m; // OK
And also this code works fine:
#include <vector>
using namespace std;
vector<int> v; // OK
I know that constructor of std::vector has two arguments (type and allocator), but why vector behaviour is so different from other containers?
UPD: I'm sorry, this is my mistake. Actually the code does compile, but CLion marks it as an error. So it is CLion's bug.
It is yet not fixed CLion bug: https://youtrack.jetbrains.com/issue/CPP-5758#u=1454575544687.
As a workaround you can try to use libstdc++ instead of libc++, see https://youtrack.jetbrains.com/issue/CPP-5758#comment=27-2389700.

Error when adding typedef in header

I'm new to C++, and I'm learning from Accelerated C++ (for anyone with the book, I'm trying to run the program described in §7.4)
The program I'm looking at uses some typedefs - I gather that if I add these to a header file, any source file which includes that header will be able to use the typedefs too.
My header is:
#ifndef READ_GRAMMAR_H_INCLUDED
#define READ_GRAMMAR_H_INCLUDED
typedef std::vector<std::string> Rule;
typedef std::vector<Rule> Rule_collection;
typedef std::map<std::string, Rule_collection> Grammar;
Grammar read_grammar(std::istream& in);
#endif // READ_GRAMMAR_H_INCLUDED
This is giving me the error error: 'map' in namespace 'std' does not name a type
If I change the third typedef to typedef std::vector<Rule_collection> Grammar; (not that I want this, just for example) it builds with no errors.
Any idea what the problem is? I have no idea whether I'm doing something trivial the wrong way, or whether the whole approach is incorrect
It says it cannot find map in namespace std. You need to include it so that the compiler can find it. Similarly, you'll need to include headers for std::vector std::string and std::istream:
#ifndef READ_GRAMMAR_H_INCLUDED
#define READ_GRAMMAR_H_INCLUDED
#include <map>
#include <vector>
#include <string>
#include <istream>
typedef std::vector<std::string> Rule;
typedef std::vector<Rule> Rule_collection;
typedef std::map<std::string, Rule_collection> Grammar;
Grammar read_grammar(std::istream& in);
#endif // READ_GRAMMAR_H_INCLUDED
If you feel courageous, you might also want to read about forward declarations - their usage, pros and cons, but I doubt it's needed in this particular case.
You must include the header files, if you don't have them included then how is your program going to use it?
#ifndef READ_GRAMMAR_H_INCLUDED
#define READ_GRAMMAR_H_INCLUDED
#include <istream>
#include <string>
#include <vector>
#include <map>
typedef std::vector<std::string> Rule;
typedef std::vector<Rule> Rule_collection;
typedef std::map<std::string, Rule_collection> Grammar;
Grammar read_grammar(std::istream& in);
#endif // READ_GRAMMAR_H_INCLUDED

C++ unordered map error

I'm trying to declare an unordered map into my program in which I will map them to tokens in another file.
I need a method which returns the Token type found in Token.h (which is an enum class)
What is confusing me is that, since I want to return the mapped Tokens from the unordered_map to the enum class, what should be the return type of the method? Also, it is stating that
error: 'unordered_map' does not name a type
I am rather new to C++ and am still finding it a bit hard in this case how I should declare methods. I've read that the unordered map should be declared INSIDE a method, but since I want the value returned by the map, which should be the return type?
I tried this
Test 1
Token Lexer::getTokenType()
{
unordered_map<string,Token> tokenType;
}
This outputs these errors:
Test 2 I tried this
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <wctype.h>
#include <map>
#include "lexer.h"
using namespace std;
long Row, Col, Offset;
unordered_map<string, Token> ProtectedWords
{
}
OR
unordered_map<string, Token>::Lexer::getTokenType()
{
}
still yielded the same
Its error:
I know these do sound stupid, but would you mind explain to me please? As in the tutorial I followed many are, yes, called inside a method, but even that did not work
You need to include <unordered_map>.
You'll also need to enable C++11 support, if you haven't already done so: for GCC, make sure the compiler arguments include -std=c++11 (or c++0x if you're using an old compiler).

std::unordered_map not compiling with icc

Like the title says, unordered_map isn't compiling for me. I get an error saying "error: namespace "std" has no member "unordered_map""
I'm compiling with this command icc test.cpp -std=c++0x
This is the program I'm trying to compile:
#include <stdio.h>
#include <string>
int main()
{
std::unordered_map<string, int> map;
}
#include <unordered_map>
See: http://en.cppreference.com/w/cpp/container/unordered_map
Defined in header <unordered_map>

How do I make a recursive boost::variant which works with gcc 4.6?

I am decoding bencode, and have some code which works well with gcc 4.4. But having recently upgraded to gcc 4.6 this code no longer builds:
#ifndef BENCODE_VALUETYPES_H
#define BENCODE_VALUETYPES_H
#include <boost/variant.hpp>
#include <string>
#include <vector>
#include <map>
namespace bencode {
typedef boost::make_recursive_variant<
int,
std::string,
std::vector<boost::recursive_variant_>,
std::map<std::string, boost::recursive_variant_> >::type Value;
typedef std::map<std::string, Value> ValueDictionary;
typedef std::vector<Value> ValueVector;
};
#endif
g++ gives this error message:
/usr/include/c++/4.6/bits/stl_pair.h: In instantiation of 'std::pair<const std::basic_string<char>, boost::recursive_variant_>':
Decoder.cpp:97:39: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:93:11: error: 'std::pair<_T1, _T2>::second' has incomplete type
/usr/include/boost/variant/variant_fwd.hpp:232:12: error: forward declaration of 'struct boost::recursive_variant_'
The documentation for the latest boost version (1.48 at the moment) states that "due to standard conformance issues in several compilers, make_recursive_variant is not universally supported", and that you should use the recursive_wrapper instead. But I am having problem making the change: does anyone know what this should look like using the wrapper?
Try defining below in your header file before you include boost variant headers.
#define BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT
#include <boost/variant.hpp>
I had the same issue and found solution at boost variant recursive