std::unordered_map not compiling with icc - c++

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>

Related

C++ Linking to separate .cpp file says "multiple definitions"

I'm very new to C++ and could use some help. I'm trying to link a file my_help_fxns.cpp to my main.cpp file so i can use those functions in main.cpp, but when i try linking I get the following error for each function in my_help_fxns:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\ccaPL79E.o:data_vars_class.cpp:(.text+0x0): multiple definition of `my_help_fxns::print_vector_items_int_type(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<int, std::allocator<int> >)'; C:\Users\Geoff\AppData\Local\Temp\cc0mRP1w.o:main.cpp:(.text+0x0): first defined here
So it says I'm defining twice, but I don't know how to get around this. I have a class called data_vars_class. i include my_help_fxns at the top of data_vars_class.cpp, and use the helper fxns successfully in methods for that class. An instance of the class is created at the top of main.cpp. however if i try to use the helper functions in main() in main.cpp, without declaring "my_help_fxns.cpp" at the top of main.cpp, it says functions arent found, and if i do declare it at the top of main.cpp, i get the duplication error its been declared twice. How can I fix this, thanks!
this is the structure of my project
main.cpp ==>
#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.cpp" <--- including here gives duplication error, but if i dont, its functions not found error
DataVars dataVars;
int main () {
my_help_fxns::pause_program();
return 0;
}
data_vars_class.hpp ==>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
class DataVars
{
private:
...
public:
...
}
data_vars_class.cpp ==>
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>
#include "data_vars_class.hpp"
#include "my_help_fxns.cpp"
...i can use my_help_fxns here with no problem, as an instance of this class is created before main() in main.cpp
my_help_fxns.cpp ==>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
namespace my_help_fxns
{
void pause_program() {
std::string dummy;
std::cout << "Enter to continue..." << std::endl;
std::getline(std::cin, dummy);
}
}
And here is the build command for the file in Geany:
g++ main.cpp data_vars_class.cpp -o a.out
Thanks for your help!
Don't include the my_help_fxns.cpp into the other CPP files since that will effectively define those functions in all the CPP files. This violates the one definition rule.
Instead
create a header file that declares (but not defines) those functions
include that header file in all the CPP files
add my_help_fxns.cpp to the compilation command line
Make changes to your files as described:
main.cpp
#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.hpp" // change file extension from cpp -> hpp
DataVars dataVars;
int main () {
my_help_fxns::pause_program();
return 0;
}
data_vars_class
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>
#include "data_vars_class.hpp"
// #include "my_help_fxns.cpp" --> Not required here
And then you may simply run:
g++ -o a.out main.cpp; ./a.out
Gives here:
Enter to continue...
sdfsdfsdfsd // --- INPUT

Why is 'stod' still not declared in this scope even after C++11 is enabled and I have included string?

I am having an error while trying to convert a string vector to a double vector. The error keeps saying:
error: 'stod' was not declared in this scope
Even though I enabled C++11 for my compiler and I used #include <string> I also used using namespace std; And it still didn't work.
The code is is down below:
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
using stod;
transform(userNums.begin(), userNums.end(), back_inserter(convUserNums), [](const string & astr){ return stod( astr) ; } ) ;
stod is defined in the namespace std. You should call it like std::stod.
Or put using namespace std; above.
Be sure to include the necessary header and using directive.
#include <string>
using std::stod;
int main( const int, const char** )
{
stod( "3.4" );
return EXIT_SUCCESS;
}
Please find the std::stod documentation here. Note that this function will throw both std::invalid_argument and std::out_of_range exceptions. A try-catch block is highly recommended.
The example was built with g++ -o main main.cpp -std=c++11 under gcc version 4.9.2 (Debian 4.9.2-10).

‘hash’ is already declared in this scope using tr1::hash;

I am trying to compile C++ code shown below but I got an error saying,
In file included from src/LM.h:3:0,
from src/LM.cpp:1:
src/common.h:30:13: error: ‘hash’ is already declared in this scope
using tr1::hash;
This is the command I used to compile the files below.
g++ -std=c++11 -Wall src/Foo.cpp
Foo.cpp
#include "Foo.h"
...
Foo.h
#ifndef FOO_H
#define FOO_H
#include "common.h"
//more code here
#endif
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include <iostream>
#include <fstream>
#include <cmath>
#include <cassert>
#include <cstdlib>
#include <utility>
#include <vector>
#include <string>
#include <array>
#include <algorithm>
#include <set>
#include <tr1/unordered_map>
#include <tr1/functional>
namespace std {
using tr1::unordered_map;
using tr1::hash;
} // namespace std
using namespace std;
//more code here
#endif
I want the source code to use std::tr1::unordered_map and std::tr1::hash rather than std::unordered_map and std::hash(Actually I am making some modifications to distributed files which does uses std::tr1::unordered_map and std::tr1::hash).
What is possibly wrong with my codes?
UPD:
https://github.com/clab/fast_align/blob/master/src/port.h seems to do the same thing as mine. However, this compiles without any problem... Have any idea?
There is already std::hash in C++11. You cannot redefine it. You can use another name for tr1::hash.
Probably the best idea (if you really want to use std::tr1::hash/std::tr1::unordered_map instead of C++11 structures) is to write your own namespace in which using all structures, that you want without std::hash/std::unordered_map.
namespace common
{
using std::tr1::hash;
using std::tr1::unordered_map;
using std::vector;
// and so on
}

Unordered Maps on g++

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

Boost library simple compilation with tuples

Can anyone tell me why this works (compiles and runs)
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
//#include <boost/tuple.hpp>
//#include <boost/tuple_comparison.hpp>
//#include <boost/tuple_io.hpp>
using namespace boost;
int main ( )
{
// tuple<int, char, float> t(2, 'a', 0.9);
// std::cout << t << std::endl;
boost::numeric::ublas::matrix<double> m1;
return 0;
}
but when I uncomment the lines related to tuple, it says
boost_tuple.cpp:3:27: fatal error: boost/tuple.hpp: No such file or directory
compilation terminated.
I use the following to compile in both cases:
g++ -Wall -c -I/usr/include/boost boost_tuple.cpp
And I also checked that /usr/include/boost/tuple.hpp exists
If you want to include it as:
#include <boost/tuple.hpp>
lose the boost part in -I/usr/include/boost. Right now this is being evaluated as /usr/include/boost/boost/tuple.hpp, which is probably your problem.
EDIT:
Make sure tuple.hpp is installed where you said it is, because on my system it's located in another folder: /usr/include/boost/tuple/tuple.hpp
Which means that I need to include the file as #include <boost/tuple/tuple.hpp> and compile it as:
g++ tuple.cpp -o tuple -I /usr/include/boost
tuple.cpp:
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/tuple/tuple.hpp>
using namespace boost;
int main ( )
{
tuple<int, char, float> t(2, 'a', 0.9);
boost::numeric::ublas::matrix<double> m1;
return 0;
}