-1

EDIT: the source of the error was a typo. Apologies for the confusion, but thanks to the many people who contributed valuable peripheral advise in comments.

The below code

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <cfloat>
#include <Rcpp.h>
#include <cassert>
using namespace Rcpp;

int test(){
  std::string s(2,"");
  return 1;
}

induces the following IDE error:

no matching constructor for initialization of 'std::string'

Running the code in R generates an inexpressive seg fault, but I hope the above is sufficient. I see that in this discussion the consensus is that this may happen when the used clang version is outmoded (clang++3.7 being the passing build). Having referred to this discussion and seeing that my machine (macos) is running the following clang:

Apple clang version 11.0.3 (clang-1103.0.32.59)

I'm not sure that the issue I'm running into is a duplicate of the one featured in the above discussion.

If I had to guess, the source of the issue is likely the compiler version, something about rcpp, or a library conflict.

EDIT: here and here are the sources which led me to believe that std ships with a default string constructor (see fill (6) for latter)

3
  • 1
    What are you trying to do with your string constructor? And which of the many overloads do you think should match the (int, const char*) argument list? Commented Sep 8, 2020 at 16:21
  • 1
    As an aside, you don't need all the extra include statements as including Rcpp.h will pull these in---it certainly does so for string. Commented Sep 8, 2020 at 16:32
  • Vote to lose as non-reproducable / typo, as OP got confused about "" and '' (see answer below). Commented Sep 8, 2020 at 16:33

1 Answer 1

1

Well?

  std::string s(2,"");

no matching constructor for initialization of 'std::string'

Exactly what it says in the error message, std::string does not have a constructor that matches your int, char const * arguments.

If I had to guess, the source of the issue is likely the compiler version, something about rcpp, or a library conflict.

No, the issue is that there is no constructor matching your initialization attempt of std::string. If you had attempted to reduce the example even further -- i.e., removing all includes other than the one you are actually using (<string>), that might have become apparent.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi DevSolar, thanks for the quick answer. I edited the answer with sources to which I had referred when surmising that there exists such a constructor
@JoshPurtell: That constructor variant ou pointed out takes a character as second argument, not a string literal. That would then have to be s(2, '\0') (which is a questionable choice for an initialization; why would you want that?). -- Sidenote: cppreference.com is generally considered to be a higher quality resource than cplusplus.com, which has somewhat of a history of being just slightly wrong here and there...
Ok, perfect. It's been a while since I've used c++ and the difference between ' and '' slipped my mind. I assume (?) that it would be best for me to delete the answer, but if you prefer I heavily edit it to reflect the basic nature of the error so that your answer is not lost, let me know.
@JoshPurtell: Nah, it's good. I'll vote for close as "typo", and that will probably be it. You shouldn't delete questions that are already answered. The SO engine frowns on that.
@JoshPurtell: Uh... uh... C++ is very much not a "trial & error" language... just because it stopped blowing up in your face doesn't mean that UB went away, and / or won't blow up in your face at some later point. I seriously doubt that something that got "fixed" by an initialization like that is correct code. Perhaps post another question on that...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.