-2

How can I avoid the error error: ‘String’ does not name a type (String, not string)

I have this MWE that works with Rcpp (this code is called from R via Rcpp)

#include <Rcpp.h>
#include <vector>
#include <set>
CharacterVector some_function(String& name) {
 // do something
}

But if I use cpp11 then it doesn't work

#include <cpp11.hpp>
#include <vector>
#include <set>
strings some_function(String& name) {
 // do something but gives and error because of the "String"
}
pachadotdev
  • 3,345
  • 6
  • 33
  • 60
  • 7
    `String` is not a standard type, how are we supposed to know what you need to include to get it? – Borgleader Jan 23 '23 at 04:24
  • Now if you actually meant C++/CLI rather than C++, I believe that there is such a thing in that language setup. However, I am not familiar with the C++/CLI language. In standard C++and its standard library it's not a thing, as @Borgleader said, though it could be something in some 3rd party library. – Avi Berger Jan 23 '23 at 04:49
  • So sorry! I don't know much about C++ about linear algebra. Now I edited the question to write something that makes more sense, and I explained when and why it works and when it doesn't. – pachadotdev Jan 25 '23 at 03:03
  • 3
    pretty sure [this Rcpp::String](https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/String.h) is the header you want. It doesn't work in your `cpp11` example because `cpp11` doesn't use it. – tospig Jan 25 '23 at 08:29
  • 1
    It's a bit unfair that this question got closed; the `String` type is actually mentioned in the cpp11 vignette - however it's not implemented in the library at all. To convert to a single string object, you can use `std::string` as an argument and cpp11 will convert. – stephematician May 14 '23 at 02:21

1 Answers1

3

To let the C++ compiler know what a String is, you'll need to #include whatever .h or .hpp file you have that declares the String class. (If you don't know which header that is, you can find out by consulting the documention of whatever API it is you are using that provides that class, or alternatively by doing a grep -R 'class String' . in the library's include folder to find out where it is declared)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234