initialising a 4d vector - vector

I wrote this code:
#include<vector>
using namespace std;
int i;
int j;
cin>>i>>j;
vector<vector<vector<vector<double> > > > boy_max(i,vector<double>(i,vector<double>(j,vector<double>(j,0))));
During compilation the following error came up:
prog.cpp: In function ‘int main()’:
prog.cpp:164: error: no matching function for call to ‘std::vector<double, std::allocator<double> >::vector(int&, std::vector<double, std::allocator<double> >)’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_vector.h:247: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = double, _Alloc = std::allocator<double>]
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_vector.h:234: note: std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = double, _Alloc = std::allocator<double>]
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_vector.h:221: note: std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = double, _Alloc = std::allocator<double>]
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_vector.h:213: note: std::vector<_Tp, _Alloc>::vector() [with _Tp = double, _Alloc = std::allocator<double>]

Your initialization line is wrong. It should be more like this:
vector<vector<vector<vector<double> > > > boy_max(i,vector<vector<vector<double> > >(i,vector<vector<double> >(j,vector<double>(j,0))));

Related

Why does my RcppParallel implementation of a user-defined function crash unexpectedly?

I have developed a dual chain markov monte carlo model designed to forecast loan portfolios in the excellent package Rcpp but have run into an issue trying to implement a parallelised version of these functions with RcppParallel.
I have based my attempts this far on this vignette (https://gallery.rcpp.org/articles/parallel-distance-matrix/) and this stackoverflow thread (How to call user-defined function in RcppParallel?).
All of the UDFs underlying this logic are implemented using Armadillo type objects, which I understand are threadsafe, and the writing of data between functions and pre-allocated outputs should be working smoothly as I have this same logic implemented successfully in serial functions. It's also true that the function portfolio_simulation_rating_model_rs_ts works well with the inputs used outside of the RcppParallel wrapper and there are no compilation errors or warnings when I source this code and the underlying functions. However, once I get to running the dcmcmc_portfolio_rating_model_parallel function in R, my session crashes only saying that there has been a fatal error.
Clearly I am missing something in the parallelisation, so any help/suggestions would be greatly appreciated.
// [[Rcpp::depends(RcppArmadillo, RcppParallel)]]
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <RcppArmadillo.h>
#include <RcppParallel.h>
using namespace arma;
using namespace RcppParallel;
using namespace Rcpp;
using namespace std;
struct dcmcmc_portfolio_rating_model_worker : public Worker {
// Input Values
const int n_loans ;
const int n_regime ;
const int n_matrix ;
const int n_amort ;
const RVector<double> loan_ids ;
const RVector<double> starting_balances ;
const RVector<double> starting_positions ;
const RVector<double> cprs ;
const RVector<double> sim_regime_indices ;
const RVector<double> loan_regime_indices ;
const RVector<double> starting_periods ;
const RVector<double> regime_matrix_indices ;
const RVector<double> matrix_indices ;
const RVector<double> matrix_elements ;
const int nrow ;
const int ncol ;
const RMatrix<double> amortisation_schedules ;
const int periods ;
const int iterations ;
// Output Matrix
RMatrix<double> output_mx ;
dcmcmc_portfolio_rating_model_worker(
const int& n_loans,
const int& n_regime,
const int& n_matrix,
const int& n_amort,
const NumericVector& loan_ids,
const NumericVector& starting_balances,
const NumericVector& starting_positions,
const NumericVector& cprs,
const NumericVector& sim_regime_indices,
const NumericVector& loan_regime_indices,
const NumericVector& starting_periods,
const NumericVector& regime_matrix_indices,
const NumericVector& matrix_indices,
const NumericVector& matrix_elements,
const int& nrow,
const int& ncol,
const NumericMatrix& amortisation_schedules,
const int& periods,
const int& iterations,
NumericMatrix& output_mx)
: n_loans(n_loans),
n_regime(n_regime),
n_matrix(n_matrix),
n_amort(n_amort),
loan_ids(loan_ids),
starting_balances(starting_balances),
starting_positions(starting_positions),
cprs(cprs),
sim_regime_indices(sim_regime_indices),
loan_regime_indices(loan_regime_indices),
starting_periods(starting_periods),
regime_matrix_indices(regime_matrix_indices),
matrix_indices(matrix_indices),
matrix_elements(matrix_elements),
nrow(nrow),
ncol(ncol),
amortisation_schedules(amortisation_schedules),
periods(periods),
iterations(iterations),
output_mx(output_mx) {}
// Setting up functions to convert inputs to arma
arma::vec convert_input_vector(RVector<double> input_vector, int length)
{RVector<double> tmp_input_vector = input_vector ;
arma::vec input_vector_ts(tmp_input_vector.begin(), length, false) ;
return input_vector_ts ;}
arma::mat convert_input_matrix(RMatrix<double> input_matrix, int rows, int cols)
{RMatrix<double> tmp_input_matrix = input_matrix ;
arma::mat input_matrix_ts(tmp_input_matrix.begin(), rows, cols, false) ;
return input_matrix_ts ;}
// Function to iterate
void operator()(std::size_t begin, std::size_t end){
arma::vec loan_ids_ts = convert_input_vector(loan_ids, n_loans) ;
arma::vec starting_balances_ts = convert_input_vector(starting_balances, n_loans) ;
arma::vec starting_positions_ts = convert_input_vector(starting_positions, n_loans) ;
arma::vec cprs_ts = convert_input_vector(cprs, n_loans) ;
arma::vec sim_regime_indices_ts = convert_input_vector(sim_regime_indices, n_regime);
arma::vec loan_regime_indices_ts = convert_input_vector(loan_regime_indices, n_regime) ;
arma::vec starting_periods_ts = convert_input_vector(starting_periods, n_regime) ;
arma::vec regime_matrix_indices_ts = convert_input_vector(regime_matrix_indices, n_regime);
arma::vec matrix_indices_ts = convert_input_vector(matrix_indices, n_matrix) ;
arma::vec matrix_elements_ts = convert_input_vector(matrix_elements, n_matrix) ;
arma::mat amortisation_schedules_ts = convert_input_matrix(amortisation_schedules, n_amort, 3) ;
for(unsigned int i = begin; i < end; i++){
arma::vec i_sim_regime_indices = allwhich_ts(sim_regime_indices_ts,
i) ;
int sim_begin = as_scalar(i_sim_regime_indices.head(1)) ;
int sim_end = as_scalar(i_sim_regime_indices.tail(1)) ;
arma::vec i_loan_regime_indices = loan_regime_indices_ts.subvec(sim_begin, sim_end) ;
arma::vec i_starting_periods = starting_periods_ts.subvec(sim_begin, sim_end) ;
arma::vec i_regime_matrix_indices = regime_matrix_indices_ts.subvec(sim_begin, sim_end) ;
arma::mat pf_simulation = portfolio_simulation_rating_model_rs_ts(
loan_ids_ts,
starting_balances_ts,
starting_positions_ts,
cprs_ts,
i_loan_regime_indices,
i_starting_periods,
i_regime_matrix_indices,
matrix_indices_ts,
matrix_elements_ts,
nrow,
ncol,
amortisation_schedules_ts,
periods
) ;
int sim_rows = pf_simulation.n_rows ;
int sim_cols = pf_simulation.n_cols ;
for(int c = 0; c < sim_cols; c++){
for(int r = 0; r < sim_rows; r++){
output_mx((n_loans*periods*i + r), c) = pf_simulation(r, c) ;
}
}
for(int r = 0; r < sim_rows; r++){
output_mx((n_loans*periods*i + r), 7) = (i + 1) ;
}
}
}
};
//[[Rcpp::export]]
NumericMatrix dcmcmc_portfolio_rating_model_parallel(
const NumericVector& loan_ids,
const NumericVector& starting_balances,
const NumericVector& starting_positions,
const NumericVector& cprs,
const NumericVector& sim_regime_indices,
const NumericVector& loan_regime_indices,
const NumericVector& starting_periods,
const NumericVector& regime_matrix_indices,
const NumericVector& matrix_indices,
const NumericVector& matrix_elements,
int nrow,
int ncol,
const NumericMatrix& amortisation_schedules,
int periods,
int iterations
){
int n_loans = loan_ids.size() ;
int n_regime = sim_regime_indices.size() ;
int n_matrix = matrix_indices.size() ;
int n_amort = amortisation_schedules.nrow() ;
NumericMatrix output_mx(n_loans*periods*iterations, 8) ;
// Creating Worker object
dcmcmc_portfolio_rating_model_worker DCMCMC(
n_loans,
n_regime,
n_matrix,
n_amort,
loan_ids,
starting_balances,
starting_positions,
cprs,
sim_regime_indices,
loan_regime_indices,
starting_periods,
regime_matrix_indices,
matrix_indices,
matrix_elements,
nrow,
ncol,
amortisation_schedules,
periods,
iterations,
output_mx
) ;
// Call parellised worker
parallelFor(0, iterations, DCMCMC) ;
return(output_mx) ;
}
EDIT:
I have produced a minimum reproducible example, trying to incorporate the helpful comments recieved on this post so far. The example sets up trivial functions designed to mimic the structure of my modelling functions. The final function causing a crash takes three vectors, vec1, vec2, and vec_ind. It applies a worker which attempts to take chunks of equal size (indentified by indices stored in vec_ind) of vec1 and vec2, add these subvector chunks, and store the results in the relevant portions of an output vector.
I have reproduced the example below using both arma::vec and std::vector types and experience the crashing behaviour in both. I present the std::vector code below, further to Dirk's suggestion that the RcppArmadillo types may be relying on R memory, and I have removed all namespace inclusions other than RcppParallel to avoid conflicts, as per onyambu's remark.
Here is the Rcpp
// [[Rcpp::depends(RcppArmadillo, RcppParallel)]]
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <RcppArmadillo.h>
#include <RcppParallel.h>
using namespace RcppParallel;
//[[Rcpp::export]]
std::vector<double> allwhich_ts(std::vector<double> vector, double value){
int length = vector.size() ;
std::vector<double> values(0) ;
for(int i = 0; i < length; i++){
bool match = vector[i] == value;
if(match){
values.push_back(i);
}
}
return(values);
}
//[[Rcpp::export]]
std::vector<double> vector_addition(std::vector<double> vector1, std::vector<double> vector2){
int n_elements = vector1.size() ;
std::vector<double> output_vec = std::vector<double>(n_elements) ;
for(int i = 0; i < n_elements; i++){
output_vec[i] = vector1[i] + vector2[i] ;
}
return(output_vec) ;
}
struct vector_addition_worker : public Worker {
const RVector<double> vector1 ;
const RVector<double> vector2 ;
const RVector<double> vector_indices ;
const int vector_length ;
RVector<double> output_vec ;
vector_addition_worker(
const Rcpp::NumericVector& vector1,
const Rcpp::NumericVector& vector2,
const Rcpp::NumericVector& vector_indices,
const int& vector_length,
Rcpp::NumericVector& output_vec
) : vector1(vector1),
vector2(vector2),
vector_indices(vector_indices),
vector_length(vector_length),
output_vec(output_vec) {}
std::vector<double> convert_input_vec(RVector<double> input_vector, int vec_length){
RVector<double> tmp_vector = input_vector ;
std::vector<double> input_vector_ts(tmp_vector.begin(), tmp_vector.end()) ;
return(input_vector_ts) ;
}
void operator()(std::size_t begin, std::size_t end){
std::vector<double> vector1_ts = convert_input_vec(vector1, vector_length) ;
std::vector<double> vector2_ts = convert_input_vec(vector2, vector_length) ;
std::vector<double> vector_indices_ts = convert_input_vec(vector_indices, vector_length) ;
for(unsigned int i = begin; i < end; i++){
std::vector<double> indices = allwhich_ts(vector_indices_ts, i) ;
int values_begin = indices.at(1) ;
int values_end = indices.at(std::distance(indices.begin(), indices.end())) ;
std::vector<double> values1(vector1_ts.begin() + values_begin, vector1_ts.begin() + values_end) ;
std::vector<double> values2(vector2_ts.begin() + values_begin, vector2_ts.begin() + values_end) ;
std::vector<double> interim_op = vector_addition(values1, values2) ;
int op_size = interim_op.size() ;
for(int n = 0; n < op_size; n++){
output_vec[i*op_size + n] = interim_op[n] ;
}
}
}
};
//[[Rcpp::export]]
Rcpp::NumericVector vector_addition_parallel(Rcpp::NumericVector vec1,
Rcpp::NumericVector vec2,
Rcpp::NumericVector vec_ind){
int vec_length = vec1.size() ;
double n_indices = *std::max_element(vec_ind.begin(), vec_ind.end()) ;
Rcpp::NumericVector op_vec(vec_length);
vector_addition_worker vec_add_worker(
vec1,
vec2,
vec_ind,
vec_length,
op_vec
) ;
parallelFor(0, n_indices, vec_add_worker) ;
return(op_vec) ;
}
Here is the R code which tests for expected behaviour
library(Rcpp)
library(RcppParallel)
library(RcppArmadillo)
# Setting up dummy data
vec1 = rep(1, 500)
vec2 = rep(1, 500)
vec_inds = sort(rep(1:20, 25))
length(vec1);length(vec2);length(vec_inds)
## Checking that allwhich_ts is working as expected
allwhich_ts(vec_inds, 1)
# Checking that vector_addition is working as expected
vector_addition(vec1, vec2)
# Checking that the same logic can be applied serially (mainly to verify data handling method)
r_repro_function <- function(vec1, vec2, vec_inds){
op_vec = numeric(length(vec1))
for(i in unique(vec_inds)){
tmp1 = vec1[vec_inds == i]
tmp2 = vec2[vec_inds == i]
tmp_op = tmp1 + tmp2
for(n in 1:length(tmp1)){
op_vec[(i - 1)*length(tmp1) + n] = tmp_op[n]
}
}
op_vec
}
r_repro_function(vec1, vec2, vec_inds)
vector_addition_parallel(vec1 = vec1,
vec2 = vec2,
vec_ind = vec_inds)
So following Dirk's suggestion I am posting an answer with a pared back example to illustrate the problem I had and the solution I arrived at with his help.
The mistake I made was actually in how I treated the begin and end variables within my worker. In contrast to the articles in the RcppParallel gallery, I was not using begin/end to guide iterators to the relevant portions of the calculation, but rather trying to use them to index the relevant part of my input dataset for each portion.
This caused dimension errors, which on my machine simply crashed the R session.
The solution to this mistake would be to either (1) ensure any UDFs you are applying deal in iterators rather than vector values or (2) to bridge the begin/end variables correctly to the vectors you are trying to index.
Given that all of my modelling functions are already in the business of taking vector indices, I have applied the second approach and create a unique_indices vector within my function which the begin/end values can simply select values from. The current solution makes some assumptions about how the input indices will work (i.e. simply integer values from smallest to largest in the argument vector).
Apologies if this is still considered verbose, but I thought it worth keeping the data-handling logic as it was in the problem statement because that is where the problem arose. That is where a submatrix is identified by an index and used as the arguments to some calculation. The key differences to the example above are on lines 48-52 and 62-65
Where (1) each i between begin and end is used to select an index as so int index_value = unique_indices[i] ; which then identifies the relevant input data and (2) the unique_indices vector is defined by the characteristics of the vector of indices vec_ind
:
// [[Rcpp::depends(RcppArmadillo, RcppParallel)]]
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <RcppArmadillo.h>
#include <RcppParallel.h>
using namespace RcppParallel;
//[[Rcpp::export]]
std::vector<double> allwhich_ts(std::vector<double> vector, double value){
int length = vector.size() ;
std::vector<double> values(length) ;
int matches = 0;
for(int i = 0; i < length; i++){
bool match = vector[i] == value;
if(match){values[matches] = i;
matches++ ;}}
std::vector<double> op(values.begin(), values.begin() + matches) ;
return(op);
}
struct vector_double_worker : public Worker {
// Defining worker arguments
const RVector<double> vector1 ;
const RVector<double> vector_indices ;
const RVector<double> unique_indices ;
const int vector_length ;
RVector<double> output_vec ;
// Initialising function argument values
vector_double_worker(
const Rcpp::NumericVector& vector1, const Rcpp::NumericVector& vector_indices,
const Rcpp::NumericVector& unique_indices, const int& vector_length, Rcpp::NumericVector& output_vec
) : vector1(vector1),vector_indices(vector_indices),unique_indices(unique_indices),
vector_length(vector_length),output_vec(output_vec) {}
// Setting up conversion function so that UDFs can deal in std:: types
std::vector<double> convert_input_vec(RVector<double> input_vector, int vec_length){
std::vector<double> input_vector_ts(input_vector.begin(), input_vector.end()) ;
return(input_vector_ts) ;}
// Defining operator ranges which will breakdown the task into partitions
void operator()(std::size_t begin, std::size_t end){
// Converting input vectors to std types
std::vector<double> vector1_ts = convert_input_vec(vector1, vector_length) ;
std::vector<double> vector_indices_ts = convert_input_vec(vector_indices, vector_length) ;
// For loop to perform calculations for each element in a given partition
for(unsigned int i = begin; i < end; i++){
int index_value = unique_indices[i] ; // begin and end now used to index the vector of input indices defined outside of the function
std::vector<double> indices = allwhich_ts(vector_indices_ts, index_value) ; // identifying sub-vector indices
int values_begin = indices.at(0) ;
int values_end = indices.at(std::distance(indices.begin(), indices.end()) - 1) ; // - 1 was added to avoid dimension error
std::vector<double> values1(vector1_ts.begin() + values_begin, vector1_ts.begin() + values_end + 1) ; // + 1 was added to avoid dimension error
int op_size = values1.size() ;
for(int n = 0; n < op_size; n++){output_vec[i*op_size + n] = values1[n] * 2 ;} // Trivial example calculation
}}};
//[[Rcpp::export]]
Rcpp::NumericVector vector_double_parallel(Rcpp::NumericVector vec1, Rcpp::NumericVector vec_ind){
int vec_length = vec1.size() ; // Setting up output vector
Rcpp::NumericVector op_vec(vec_length);
double n_indices = *std::max_element(vec_ind.begin(), vec_ind.end()) ; // Identifying unique index values
double min_indices = *std::min_element(vec_ind.begin(), vec_ind.end()) ;
Rcpp::NumericVector unique_indices(n_indices) ;
std::iota(unique_indices.begin(), unique_indices.end(), min_indices);
vector_double_worker vec_2_worker(vec1,vec_ind,unique_indices,vec_length,op_vec) ; // Setting up parallel worker
parallelFor(0, n_indices, vec_2_worker) ; // Populating output vector with results
return(op_vec) ;}

How to avoid the ambiguous overloaded operator problem when using list and numeric vector in Rcpp?

Goal
The code below is a very simplified version of my original code for creating a reproducible example. In this code, I am trying to generate a vector that is filled up by using 2 values in each iteration from a for-loop. These values are first multiplied with a parameter p_BL that I provide in a parameters list. You can see that at the end of the code I multiply the parameter (from a list) to a integer in a NumericVector. That's where I get the overloaded operator error. How can I resolve this?
Code in the simple_function.cpp file
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector my_func(List parameters, int number_simulations)
{
NumericVector vector_trig_times (number_simulations);
NumericVector vector_trig_times1 (number_simulations);
NumericVector vector_trig_times2 (number_simulations);
for (int i_simulation = 0; i_simulation < number_simulations; i_simulation++) {
int vector_activation1 = i_simulation + 2;
int vector_activation2 = i_simulation + 5;
vector_trig_times1[i_simulation] = vector_activation1;
vector_trig_times2[i_simulation] = vector_activation2;
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
} // i_simulations for loop
return vector_trig_times;
}
Compiling the file:
library(Rcpp)
sourceCpp("simple_function.cpp")
Error
Note: Line 28 refers to:
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
Edit: Text of the error message
C:/RBuildTools/3.5/mingw_64/bin/g++ -I"C:/PROGRA~1/R/R-36~1.1/include" -DNDEBUG -I"C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include" -I"C:/Users/durraniu/GOOGLE~1/DISSER~1" -O2 -Wall -mtune=generic -c simple_function.cpp -o simple_function.o
simple_function.cpp: In function 'Rcpp::NumericVector my_func(Rcpp::List, int)':
simple_function.cpp:28:59: error: no match for 'operator*' (operand types are 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' and 'Rcpp::traits::storage_type<14>::type {aka double}')
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
simple_function.cpp:28:59: note: candidates are:
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/RcppCommon.h:136:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:27,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/complex.h:25:17: note: Rcomplex operator*(const Rcomplex&, const Rcomplex&)
inline Rcomplex operator*( const Rcomplex& lhs, const Rcomplex& rhs) {
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/complex.h:25:17: note: no known conversion for argument 2 from 'Rcpp::traits::storage_type<14>::type {aka double}' to 'const Rcomplex&'
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:473:1: note: template<int RTYPE, bool LHS_NA, class LHS_T, bool RHS_NA, class RHS_T> Rcpp::sugar::Times_Vector_Vector<RTYPE, LHS_NA, LHS_T, RHS_NA, RHS_T> Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const Rcpp::VectorBase<RTYPE, RHS_NA, RHS_T>&)
operator*(
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:473:1: note: template argument deduction/substitution failed:
simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>'
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:459:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive_nona<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::sugar::NonaPrimitive<U>&, const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&)
operator*(
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:459:1: note: template argument deduction/substitution failed:
simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::sugar::NonaPrimitive<U>'
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:450:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive_nona<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const Rcpp::sugar::NonaPrimitive<U>&)
operator*(
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:450:1: note: template argument deduction/substitution failed:
simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>'
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:439:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive<RTYPE, NA, T> >::type Rcpp::operator*(const U&, const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&)
operator*(
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:439:1: note: template argument deduction/substitution failed:
simple_function.cpp:28:92: note: mismatched types 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>' and 'Rcpp::traits::storage_type<14>::type {aka double}'
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/operators.h:33:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/sugar.h:30,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:74,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:429:1: note: template<int RTYPE, bool NA, class T, class U> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<U>::type, typename Rcpp::traits::storage_type<RTYPE>::type>::value, Rcpp::sugar::Times_Vector_Primitive<RTYPE, NA, T> >::type Rcpp::operator*(const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>&, const U&)
operator*(
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/sugar/operators/times.h:429:1: note: template argument deduction/substitution failed:
simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::VectorBase<RHS_RTYPE, RHS_NA, RHS_T>'
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/Vector.h:58:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:40,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:271:14: note: template<int RTYPE, template<class> class StoragePolicy, class T> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<T>::type, typename Rcpp::Matrix<RTYPE, StoragePolicy>::stored_type>::value, Rcpp::Matrix<RTYPE, StoragePolicy> >::type Rcpp::operator*(const T&, const Rcpp::Matrix<RTYPE, StoragePolicy>&)
operator __OPERATOR__ (const T &lhs, const Matrix<RTYPE, StoragePolicy> &rhs) { \
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:280:1: note: in expansion of macro 'RCPP_GENERATE_SCALAR_MATRIX_OPERATOR'
RCPP_GENERATE_SCALAR_MATRIX_OPERATOR(*)
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:271:14: note: template argument deduction/substitution failed:
operator __OPERATOR__ (const T &lhs, const Matrix<RTYPE, StoragePolicy> &rhs) { \
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:280:1: note: in expansion of macro 'RCPP_GENERATE_SCALAR_MATRIX_OPERATOR'
RCPP_GENERATE_SCALAR_MATRIX_OPERATOR(*)
^
simple_function.cpp:28:92: note: mismatched types 'const Rcpp::Matrix<RTYPE, StoragePolicy>' and 'Rcpp::traits::storage_type<14>::type {aka double}'
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/Vector.h:58:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:40,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:254:14: note: template<int RTYPE, template<class> class StoragePolicy, class T> typename Rcpp::traits::enable_if<Rcpp::traits::is_convertible<typename Rcpp::traits::remove_const_and_reference<T>::type, typename Rcpp::Matrix<RTYPE, StoragePolicy>::stored_type>::value, Rcpp::Matrix<RTYPE, StoragePolicy> >::type Rcpp::operator*(const Rcpp::Matrix<RTYPE, StoragePolicy>&, const T&)
operator __OPERATOR__ (const Matrix<RTYPE, StoragePolicy> &lhs, const T &rhs) { \
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:262:1: note: in expansion of macro 'RCPP_GENERATE_MATRIX_SCALAR_OPERATOR'
RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(*)
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:254:14: note: template argument deduction/substitution failed:
operator __OPERATOR__ (const Matrix<RTYPE, StoragePolicy> &lhs, const T &rhs) { \
^
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/vector/Matrix.h:262:1: note: in expansion of macro 'RCPP_GENERATE_MATRIX_SCALAR_OPERATOR'
RCPP_GENERATE_MATRIX_SCALAR_OPERATOR(*)
^
simple_function.cpp:28:92: note: 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}' is not derived from 'const Rcpp::Matrix<RTYPE, StoragePolicy>'
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
simple_function.cpp:28:100: error: ambiguous overload for 'operator-' (operand types are 'int' and 'Rcpp::Vector<19>::NameProxy {aka Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>}')
vector_trig_times[i_simulation] = (parameters["p_BL"] * vector_trig_times2[i_simulation]) + ((1-parameters["p_BL"]) * vector_trig_times1[i_simulation]);
^
simple_function.cpp:28:100: note: candidates are:
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/date_datetime.h:29:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:62,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/Datetime.h:158:20: note: double Rcpp::operator-(const Rcpp::Datetime&, const Rcpp::Datetime&)
inline double operator-(const Datetime& d1, const Datetime& d2) { return d1.m_dt - d2.m_dt; }
^
In file included from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/date_datetime.h:25:0,
from C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp.h:62,
from simple_function.cpp:1:
C:/Users/durraniu/Documents/R/win-library/3.6/Rcpp/include/Rcpp/date_datetime/Date.h:164:19: note: double Rcpp::operator-(const Rcpp::Date&, const Rcpp::Date&)
inline double operator-( const Date& d1, const Date& d2) { return d1.m_d - d2.m_d; }
^
make: *** [C:/PROGRA~1/R/R-36~1.1/etc/x64/Makeconf:215: simple_function.o] Error 1
Error in sourceCpp("simple_function.cpp") :
Error 1 occurred building shared library.
Here's how you could implement the spot-on suggestions of Roland and Ralf; notice I've also cut out the unnecessary vector creation and assignment for your i+2 and i+5 values (I also used i as a counter because I don't think i_simulation is more informative, so it makes your code less readable as the line either has to go way over 80 characters or you have to split the calculation over two lines):
#include <Rcpp.h>
// [[Rcpp::export]]
NumericVector my_func2(List parameters, int number_simulations)
{
NumericVector vector_trig_times (number_simulations);
double p_BL = as<NumericVector>(parameters["p_BL"])[0];
for ( int i = 0; i < number_simulations; ++i ) {
vector_trig_times[i] = (p_BL * (i + 5)) + ((1 - p_BL) * (i + 2));
}
return vector_trig_times;
}
This compiles just fine, and the result is as expected:
Rcpp::sourceCpp("so.cpp")
my_func2(list(p_BL = 0.5), 10)
# [1] 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5 12.5

Rcpp cannot convert ‘SEXP {aka SEXPREC*}’ to ‘double’ in initialization

I am trying to duplicate the R vectorised sum in Rcpp
I first try the following trouble-free code:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double call(NumericVector x){
return sum(x);
}
Type call(Time)
> call(Time)
[1] 1919853
Then an environment version, also works well,
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double call(){
Environment env = Environment::global_env();
NumericVector Time = env["Time"];
return sum(Time);
}
Type call()
> call()
[1] 1919853
Now I am trying something weird as following,
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double call(){
Environment base("package:base");
Function sumc = base["sum"];
Environment env = Environment::global_env();
NumericVector Time = env["Time"];
double res = sumc(Time);
return res;
}
This time I got a error message:
trycpp.cpp:10:25: error: cannot convert ‘SEXP {aka SEXPREC*}’ to ‘double’ in initialization
double res = sumc(Time);
Any idea what's going wrong ?
You cannot call an R function (ie sumc() on one of Rcpp's vectors. Do this:
// [[Rcpp::export]]
double mycall(){
Environment base("package:base");
Function sumc = base["sum"];
Environment env = Environment::global_env();
NumericVector Time = env["Time"];
double res = sum(Time);
return res;
}
Here sum() is the Rcpp sugar function.

good dictionary type for wxwidgets

What would be a good dictionary type to use with wxWidgets?
Is there such a type somewhere in the framework?
Should I just stick with arrays? I've tried unordered_map but giving me errors with the wxString type.
#include <unordered_map>
std::unordered_map<wxString,int>mapsi;
mapsi={
{"first",1},
{"second",2},
};
errors:
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h: In ins
tantiation of 'struct std::__detail::_Hash_code_base<wxString, std::pair<const w
xString, int>, std::__detail::_Select1st, std::hash<wxString>, std::__detail::_M
od_range_hashing, std::__detail::_Default_ranged_hash, true>':
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1402:10
: required from 'struct std::__detail::_Hashtable_base<wxString, std::pair<con
st wxString, int>, std::__detail::_Select1st, std::equal_to<wxString>, std::hash
<wxString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_ha
sh, std::__detail::_Hashtable_traits<true, false, true> >'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:174:11: requ
ired from 'class std::_Hashtable<wxString, std::pair<const wxString, int>, std::
allocator<std::pair<const wxString, int> >, std::__detail::_Select1st, std::equa
l_to<wxString>, std::hash<wxString>, std::__detail::_Mod_range_hashing, std::__d
etail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail:
:_Hashtable_traits<true, false, true> >'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\unordered_map.h:100:18:
required from 'class std::unordered_map<wxString, int>'
E:\bootsi\New folder\TEST.cpp:96:34: required from here
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1070:12
: error: invalid use of incomplete type 'struct std::hash<wxString>'
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1070:12
: error: invalid use of incomplete type 'struct std::hash<wxString>'
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1082:53
: error: invalid use of incomplete type 'struct std::hash<wxString>'
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1082:53
: error: invalid use of incomplete type 'struct std::hash<wxString>'
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
E:\bootsi\New folder\TEST.cpp: In constructor 'std::unordered_map<_Key,
_Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash,
_Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type
&) [with _Key = wxString; _Tp = int; _Hash = std::hash<wxString>; _Pred = std::e
qual_to<wxString>; _Alloc = std::allocator<std::pair<const wxString, int> >; std
::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type = unsigned int; std:
:unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hasher = std::hash<wxString>; s
td::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<wx
String>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::allocator_type = s
td::allocator<std::pair<const wxString, int> >]':
E:\bootsi\New folder\TEST.cpp:96:34: error: invalid use of incomplete t
ype 'std::unordered_map<wxString, int>::hasher {aka struct std::hash<wxString>}'
std::unordered_map<wxString,int>mapsi;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'std::unordered_map<wxString, int>::hasher {aka struct std:
:hash<wxString>}'
struct hash;
E:\bootsi\New folder\TEST.cpp:96:34: note: when instantiating default
argument for call to std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unord
ered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const h
asher&, const key_equal&, const allocator_type&) [with _Key = wxString; _Tp = in
t; _Hash = std::hash<wxString>; _Pred = std::equal_to<wxString>; _Alloc = std::a
llocator<std::pair<const wxString, int> >; std::unordered_map<_Key, _Tp, _Hash,
_Pred, _Alloc>::size_type = unsigned int; std::unordered_map<_Key, _Tp, _Hash, _
Pred, _Alloc>::hasher = std::hash<wxString>; std::unordered_map<_Key, _Tp, _Hash
, _Pred, _Alloc>::key_equal = std::equal_to<wxString>; std::unordered_map<_Key,
_Tp, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const wxSt
ring, int> >]
std::unordered_map<wxString,int>mapsi;
^
E:\bootsi\New folder\TEST.cpp: At global scope:
E:\bootsi\New folder\TEST.cpp:149:19: error: expected constructor, dest
ructor, or type conversion before ';' token
CreateStatusBar();
^
E:\bootsi\New folder\TEST.cpp:150:15: error: expected constructor, dest
ructor, or type conversion before '(' token
SetStatusText("Welcome to wxWidgets!");
^
E:\bootsi\New folder\TEST.cpp:151:1: error: expected declaration before
'}' token
}
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h: In ins
tantiation of 'std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H
2, std::__detail::_Default_ranged_hash, true>::_Hash_code_base(const _ExtractKey
&, const _H1&, const _H2&, const std::__detail::_Default_ranged_hash&) [with _Ke
y = wxString; _Value = std::pair<const wxString, int>; _ExtractKey = std::__deta
il::_Select1st; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_hashi
ng]':
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1463:65
: required from 'std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Eq
ual, _H1, _H2, _Hash, _Traits>::_Hashtable_base(const _ExtractKey&, const _H1&,
const _H2&, const _Hash&, const _Equal&) [with _Key = wxString; _Value = std::pa
ir<const wxString, int>; _ExtractKey = std::__detail::_Select1st; _Equal = std::
equal_to<wxString>; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_h
ashing; _Hash = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_H
ashtable_traits<true, false, true>]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:828:24: requ
ired from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2,
_Hash, _RehashPolicy, _Traits>::_Hashtable(std::_Hashtable<_Key, _Value, _Alloc,
_ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type, const
_H1&, const _H2&, const _Hash&, const _Equal&, const _ExtractKey&, const alloca
tor_type&) [with _Key = wxString; _Value = std::pair<const wxString, int>; _Allo
c = std::allocator<std::pair<const wxString, int> >; _ExtractKey = std::__detail
::_Select1st; _Equal = std::equal_to<wxString>; _H1 = std::hash<wxString>; _H2 =
std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash;
_RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_
Hashtable_traits<true, false, true>; std::_Hashtable<_Key, _Value, _Alloc, _Extr
actKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type = unsigned i
nt; std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
_RehashPolicy, _Traits>::allocator_type = std::allocator<std::pair<const wxStrin
g, int> >]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:397:26: requ
ired from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2,
_Hash, _RehashPolicy, _Traits>::_Hashtable(std::_Hashtable<_Key, _Value, _Alloc,
_ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type, const
_H1&, const key_equal&, const allocator_type&) [with _Key = wxString; _Value =
std::pair<const wxString, int>; _Alloc = std::allocator<std::pair<const wxString
, int> >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<wxStri
ng>; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_hashing; _Hash =
std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_reha
sh_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::_
Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPol
icy, _Traits>::size_type = unsigned int; std::_Hashtable<_Key, _Value, _Alloc, _
ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::key_equal = std::e
qual_to<wxString>; std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H
1, _H2, _Hash, _RehashPolicy, _Traits>::allocator_type = std::allocator<std::pai
r<const wxString, int> >]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\unordered_map.h:142:35:
required from 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_ma
p(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const hasher&,
const key_equal&, const allocator_type&) [with _Key = wxString; _Tp = int; _Has
h = std::hash<wxString>; _Pred = std::equal_to<wxString>; _Alloc = std::allocato
r<std::pair<const wxString, int> >; std::unordered_map<_Key, _Tp, _Hash, _Pred,
_Alloc>::size_type = unsigned int; std::unordered_map<_Key, _Tp, _Hash, _Pred, _
Alloc>::hasher = std::hash<wxString>; std::unordered_map<_Key, _Tp, _Hash, _Pred
, _Alloc>::key_equal = std::equal_to<wxString>; std::unordered_map<_Key, _Tp, _H
ash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const wxString, i
nt> >]'
E:\bootsi\New folder\TEST.cpp:96:34: required from here
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1099:63
: error: invalid use of incomplete type 'struct std::hash<wxString>'
: __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1099:63
: error: invalid use of incomplete type 'struct std::hash<wxString>'
: __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
struct hash;
There is always wxHashMap
WX_DECLARE_STRING_HASH_MAP( int, // type of the values
myMapType); // name of the class
myMapType myMap;
myMap["test"]=321;
To use wxString in a unordered_map, you will need to provide a hash function for wxString. It might be simpler to use std::wstring.
So you have the choices:
Use wxHashMap which provides support for wxString immediately, but not for all the latest C+11 goodness.
Use unordered_map with a home brewed wxString hash function.
Use unordered_map with std::wstring. You will have to convert between wxString and std::wstring, but will get complete support for C++11
Wait of wxWidgets v3.1.0
I recommend option #3, for now
Thanks to this question, wxWidgets 3.1.0 will have std::hash<wxString> specialization (see this commit, and so your example should work out of the box.

How to wrap a C function whose parameter is wchar_t pointer with cython

I want to use cython to wrap a C library. One function in the library is like
int hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen);
There are two questions:
What can I do with the wchar_t in cython;
How to convert the string pointer in my .pyx file.
Declare wchar_t:
cdef extern from "stddef.h":
ctypedef void wchar_t
Or import from libc module:
from libc.stddef cimport wchar_t
A function to convert wchar_t to python string by using WideCharToMultiByte (see CefStringToPyString):
# Declare these in .pxd file:
#
# cdef extern from "Windows.h":
# cdef int CP_UTF8
# cdef int WideCharToMultiByte(int, int, wchar_t*, int, char*, int, char*, int*)
cdef object WideCharToPyString(wchar_t *wcharstr):
cdef int charstr_bytes = WideCharToMultiByte(CP_UTF8, 0, wcharstr, -1, NULL, 0, NULL, NULL)
# Do not use malloc, otherwise you get trash data when string is empty.
cdef char* charstr = <char*>calloc(charstr_bytes, sizeof(char))
cdef int copied_bytes = WideCharToMultiByte(CP_UTF8, 0, wcharstr, -1, charstr, charstr_bytes, NULL, NULL)
if bytes == str:
pystring = "" + charstr # Python 2.7
else:
pystring = (b"" + charstr).decode("utf-8", "ignore") # Python 3
free(charstr)
return pystring
Since Python 3.2 you can do this with the use of PyUnicode_FromWideChar(wcharstr, -1), see the comment by compostus.

Resources