I am a newbie trying to learn data structure and algorithm implementation using Standard Template Library (STL) in Rcpp.
I am trying to implement a very basic hash table using unordered_map in Rcpp, taking a hint from Hadley's Advanced R
Here is the C++ code I want to implement in Rcpp (taken from element14 blog)
#include <unordered_map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
unordered_map<string, string> hashtable;
hashtable.emplace("www.element14.com", "184.51.49.225");
cout << "IP Address: " << hashtable["www.element14.com"] << endl;
return 0;
}
My Rcpp version for the same code is (please ignore the int main, I will name it int hash for now)
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <unordered_map>
#include <string>
using namespace Rcpp;
//[[Rcpp::export]]
int hash_test{
std::unordered_map<std::string, std::string> hashtable;
hashtable.emplace("www.element14.com", "184.51.49.225");
Rcout << "IP Address: " << hashtable["www.element14.com"] << endl;
return 0;
}
On running
sourceCpp("./hash_test.cpp")
I get the following errors (I am not a C++ professional, so please ignore any silly mistakes)
hash_test.cpp:11:46: error: expected primary-expression before ‘hashtable’
std::unordered_map<std::string, std::string> hashtable;
^
hash_test.cpp:11:46: error: expected ‘}’ before ‘hashtable’
hash_test.cpp:11:46: error: expected ‘,’ or ‘;’ before ‘hashtable’
hash_test.cpp:12:1: error: ‘hashtable’ does not name a type
hashtable.emplace("www.element14.com", "184.51.49.225");
^
hash_test.cpp:14:1: error: ‘Rcout’ does not name a type
Rcout << "IP Address: " << hashtable["www.element14.com"] << endl;
^
hash_test.cpp:15:1: error: expected unqualified-id before ‘return’
return 0;
^
hash_test.cpp:16:1: error: expected declaration before ‘}’ token
}
^
make: *** [hash_test.o] Error 1
Error in sourceCpp("./CDM_Open_Source_ME/kohls_model/hash_test.cpp") :
Error 1 occurred building shared library.
In addition: Warning message:
No function found for Rcpp::export attribute at hash_test.cpp:9
I frankly dont know how to debug the code. Please help.