I'am trying to compile and run a C++ program where is included boost/lexical_cast.hpp in fedora 20, where is installed boost-devel 1.50
What I get is as follow:
ina#localhost Examples]$ g++ -I ../Libraries/ quark_prop.cpp
In file included from ../Libraries/mdp.h:177:0,
from ../Libraries/fermiqcd.h:15,
from quark_prop.cpp:1:
../Libraries/mdp_utils.h:73:51: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
int is_file(string filename, char permission[]="r") {
^
In file included from ../Libraries/fermiqcd.h:15:0,
from quark_prop.cpp:1:
/usr/include/boost/assert.hpp: In function ‘void boost::assertion::detail::assertion_failed_msg(const char*, const char*, const char*, const char*, long int)’:
../Libraries/mdp.h:49:14: error: expected unqualified-id before string constant
#define endl "\n"
^
../Libraries/mdp.h:49:14: error: expected ‘;’ before string constant
While in another pc with OS ubuntu 10.04 and boost 1.40 this codes works perfectly.
Any idea of what is happening?
Thank you
Your message is unrelated to boost.
You cannot pass a string literal as char*. It's always const, so pass it as char const*: Live On Coliru
Regarding the other error in mdp.h, you need to show the relevant code
UPDATE Ah.
The problem is with the define. It's breaking the compilation of the boost header because mdp.h writes;
std::endl
somewhere, and the preprocessor is making that into
std::"\n"
which isn't valid C++.
Remove the define. Use using instead (but not in header files):
http://en.cppreference.com/w/cpp/language/using_declaration
Why is "using namespace std" considered bad practice?
Related
So I want to use char8_t data type in my code. But my compiler shows me that it could not find an identifier like char8_t , which probably means it could not find required header file or definitions for it.
So can anyone tell me what header files / definitions should I use to get the char8_t data type? The language is C++. I'll be thankful if you can also answer for C.
PS:<cwchar> did not work.
Edit:
My code:
#include <iostream>
#include <cwchar>
using namespace std;
int main()
{
char c='a';
wchar_t w=L'A';
char8_t c8=u8'c';
char16_t c16=u'D';
char32_t c32=U'K';
cout<<"Data Type"<<"\t"<<"Size"<<"\n"
<<"char"<<"\t \t"<<sizeof(c)<<" bytes"<<"\n"
<<"wchar_t"<<"\t \t"<<sizeof(w)<<" bytes"<<"\n"
<<"char8_t"<<"\t"<<sizeof(c8)<<"\n"
<<"char16_t"<<"\t"<<sizeof(c16)<<" bytes"<<"\n"
<<"char32_t"<<"\t"<<sizeof(c32)<<" bytes"<<"\n";
return 0;
}
My compiler throws this error:
WideCharacters.cpp: In function 'int main()':
WideCharacters.cpp:8:5: error: 'char8_t' was not declared in this
scope; did you mean 'wchar_t'?
8 | char8_t c8=u8'c';
| ^~~~~~~
| wchar_t
WideCharacters.cpp:15:31: error: 'c8' was not declared in this
scope; did you mean 'c'?
15 | <<"char8_t"<<"\t"<<sizeof(c8)<<"\n"
| ^~
| c
Edit-2:
I have my C++ standard set to C++20 in VS code so there is no problem with standard.
char8_t is a keyword. It's built into the language, so you don't need any headers.
If it doesn't work, either your compiler doesn't support it, or you forgot to enable C++20 support (e.g. -std=c++20 in GCC).
The problem here is possibly the compiler is not able to identify that this is a C++20 feature.
Compile using this:
g++ -Wall -std=c++20 "yourFileName".cpp
replace youFileName with that of the file name.
I'm having difficulty figuring out exactly how to use execvp in C++. I'm not having any issues getting my code to work, but I'm specifically trying to figure out how to do it in a way that doesn't make the compiler complain.
I have looked at various questions on Stack Overflow and other resources, but I have been unable to find a solution that results in zero warnings from the compiler.
Consider the following C++ program, which prints its own source code:
#include <unistd.h>
int main(int argc, char *argv[])
{
char *args[3];
args[0] = "/bin/cat";
args[1] = __FILE__;
args[2] = NULL;
execvp(args[0], args);
return 0;
}
(I know that the return 0 should never be reached; I'm not so concerned with error handling in this question.)
When I compile it, the compiler emits two warnings:
$ g++ -Wall exec.cpp
exec.cpp: In function ‘int main(int, char**)’:
exec.cpp:6:15: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
args[0] = "/bin/cat";
^~~~~~~~~~
exec.cpp:7:15: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
args[1] = __FILE__;
^~~~~~~~
The compiled program successfully prints the source file. However, I'd really like to get the program to compile without any warnings. Since the compiler doesn't like that the string literals are being assigned to a pointer of type char* (not const char*), I suppose it would make sense to mark args as an array of const char* pointers. Consider this version of the program:
#include <unistd.h>
int main(int argc, char *argv[])
{
const char *args[3];
args[0] = "/bin/cat";
args[1] = __FILE__;
args[2] = NULL;
execvp(args[0], args);
return 0;
}
I would think that this program should compile and run with no warnings or errors, but the compiler does emit an error:
$ g++ -Wall exec.cpp
exec.cpp: In function ‘int main(int, char**)’:
exec.cpp:10:25: error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]
execvp(args[0], args);
^
In file included from exec.cpp:1:0:
/usr/include/unistd.h:581:12: note: initializing argument 2 of ‘int execvp(const char*, char* const*)’
extern int execvp (const char *__file, char *const __argv[])
^~~~~~
I also tried declaring args as char const *args[3], but the compiler emits the same error. The only way I am able to get it to compile with no warnings is by casting args in the call to execvp:
const char *args[3];
...
execvp(args[0], (char* const*)args);
This version compiles without warnings and runs successfully. However, I prefer to avoid casting when I can, because it makes it harder for me to reason about the type conversions going on.
Are one of the two working ways that I have shown above the best way to create an argument array to pass to execvp, or is there a better way that is clear and does not result in the compiler complaining?
I am using two different compilers - g++ 6.2.0 for native compilation on Ubuntu x86_64, and g++ 4.5.3 for cross compilation to an ARM platform.
Edit:
I do not believe that my question is a duplicate of this question. I understand the different effects of using const in different ways with respect to a char* variable. I am specifically asking which type is conventionally used for execvp calls, which is not answered by the linked question.
I am porting some C++ code from Windows to Linux (and eventually OSX). Tons of C++ issues arise due to Windows non-compliance. I seem to have gotten past that, but am now facing a boost problem.
Basically, I want to chop up a string where the substrings of interest are separated by commas, and shove those into a string vector. This results in errors in g++, but it works fine compiling with Visual Studio
This program illustrates the issue exactly:
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
int main (void) {
std::vector<std::string> str_vec;
std::string str_to_split = "this,is,the,string,to,split";
boost::algorithm::split(str_vec,
str_to_split.substr(1, str_to_split.size()-2),
boost::algorithm::is_any_of(","),
boost::algorithm::token_compress_on);
return 0;
}
To compile I do: >> g++ -o foo foo.cpp
This is my terminal output:
foo.cpp: In function 'int main()':
foo.cpp:11:54: error: invalid initialization of non-const reference type 'std::basic_string<char>&' from an rvalue of type'std::basic_string<char>'
boost::algorithm::split(str_vec,str_to_split.substr(1, str_to_split.size()-2),boost::algorithm::is_an
^
In file included from /usr/include/boost/algorithm/string.hpp:23:0,
from foo.cpp:1:
/usr/include/boost/algorithm/string/split.hpp:140:35: note: initializing argument 2 of 'equenceSequenceT& boost::algorithm::split(SequenceSequenceT&, RangeT&, PredicateT, boost::algorithm::token_compress_mode_type) [with SequenceSequenceT = std::vector<std::basic_string<char> >; RangeT = std::basic_string<char>; PredicateT = boost::algorithm::detail::is_any_ofF<char>]'
inline SequenceSequenceT& split(
^
This function takes std::string& rather than std::string or const std::string&. That means you'll have to store the result of .substr in an intermediate variable then pass the variable to boost::algorithm::split. It'll be clearer code anyway.
FWIW, I have no idea why the function is designed this way. Seems odd to me but there you go.
Here a basic code I'm trying to run But I'm having trouble with stoi (it's c++) I keep getting error:
‘stoi’ was not declared in this scope
I tried atoi and strtol with this error
.cpp:23: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
The code:
using namespace std;
int main(){
string numberGuessed;
int intNumberGuessed = 0;
do {
cout << "Guess a numeber btw 1 - 10: " << endl;
getline(cin, numberGuessed);
intNumberGuessed = atoi(numberGuessed);
cout << intNumberGuessed << endl;
} while(intNumberGuessed != 4);
cout<< "you win" << endl;
return 0;
}
The atoi() function accepts const char* argument, but you're trying to pass it std::string. Write it like intNumberGuessed = atoi(numberGuessed.c_str()); to take the pointer.
As for the first error, about stoi() being undeclared — it is because the function was added in C++11 standard, so you have to enable its support in your compiler. I.e. in older versions of GCC you could do it with -std=c++11 option (since gcc5 C11 is enabled by default, and since gcc6 C++11 will be enabled by default).
Use stoi, it's the modern C++ version of C's atoi.
Update:
Since the original answer text above the question was amended with the following error message:
‘stoi’ was not declared in this scope
Assuming this error was produced by g++ (which uses that wording), this can have two different causes:
Using a non-conforming variant of g++ that doesn't provide std::stoi.
Using g++ in C++03 mode (stoi was introduced in C++11).
For Windows, the MinGW-w64 variant is known to provide std::stoi, and in particular the Nuwen distribution is based on MinGW-w64.
For C++11 mode, with g++ use the option -std=c++11. For example, this is necessary with the Nuwen distribution g++ version 5.1.
I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem