I am currently trying to get the following very simple boost::phoenix::lambda to compile:
#include <iostream>
#include <boost/phoenix/scope.hpp>
int main() {
boost::phoenix::lambda[std::cout << "Lambda!!"]();
}
However, this generates a host of errors (too much to post here), none which make any sense to me. Here is an excerpt of the compiler output:
error: 'std::ios_base::ios_base(const std::ios_base&)' is private
within this context
error: initializer for
'boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,
boost::proto::argsns_::term<boost::phoenix::vector0<> >, 0l>::proto_child0
{aka boost::phoenix::vector0<>}' must be brace-enclosed
I am compiling these using MinGW 4.7.2 on Windows XP with Boost 1.53.0. What am I doing wrong?
Firstly, always
#include <boost/phoenix/phoenix.hpp>
unless you know what you're doing.
Secondly, you need to make either operand of operator<< be a phoenix terminal, otherwise, it will be just
std::cout << "Lambda!!"
which is an expression of type std::ostream&...
Now, you could do anything, really, e.g.
phx::ref(std::cout) << "Lambda!!"
or
std::cout << phx::val("Lambda!!")
Either will compile.
Related
Edit: I'm trying to tell it to work with C++11 by clicking "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.
I'm getting stoi was not declared in scope, and I've added c++11 to Code::Blocks; I've added compatibility in Settings -> Compilers -> Compiler flags, but it still keeps giving me that error.
And when I try to do atoi or strtol I get the following error:
C:\Users\user\Desktop\Programming\NewProject\main.cpp|19|error: cannot
convert 'std::string {aka std::basic_string}' to 'const char*'
for argument '1' to 'long int strtol(const char*, char**, int)'|
My code:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string numberGuessed;
int numberGuessedint = 0;
do {
cout << "Guess a number between 1 and 10: ";
getline(cin, numberGuessed);
numberGuessedint = stoi(numberGuessed);
cout << numberGuessedint << endl;
} while(numberGuessedint != 4);
cout << "You win!" << endl;
return 0;
}
It is a known bug in MinGW bundled with Code::Blocks.
You can apply a patch: http://tehsausage.com/mingw-to-string
Or download fresh version of MinGW (preferable with threading support, as you lack it too) and replace one you have right now.
To use atoi you need:
numberGuessedint = atoi(numberGuessed.c_str());
I am writing a solution which worked for me. As I found in most of the solutions posted on stack overflow, code blocks earlier versions contain a bug. So I deleted my older code blocks version and installed a new version 17.12 from code blocks website.
Then I just clicked on "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.
Settings -> Compilers -> Compiler flags.
It works for me(I am using windows 7).
I'm trying to compile a simple program utilizing literals from the std::literals namespace, but Clang is generating errors when I try to compile it.
The code I'm trying to compile:
#include <string>
#include <iostream>
using namespace std::literals;
int main()
{
std::cout << "Hello World!"s << std::endl;
return 0;
}
and the compilation command:
clang++ -stdlib=libstdc++ -std=c++1y a.cpp
which leads to this output:
a.cpp:4:22: error: expected namespace name
using namespace std::literals;
~~~~~^
a.cpp:8:29: error: no matching literal operator for call to 'operator "" s' with arguments of
types 'const char *' and 'unsigned long', and no matching literal operator template
std::cout << "Hello World!"s << std::endl;
^
2 errors generated.
Using g++ or libc++ are out of the question for various reasons, and I've confirmed that other C++14 features (ie. return type deduction and binary literals) work, so it's not an issue with the compiler, making me believe it involves libstdc++.
What can I do to fix this? I'm on Linux Mint 17.1 if it makes any difference.
Remember to ensure that you're compiling the source according to C++14 (the chrono literals are not provided in C++11).
clang++ -stdlib=libstdc++ -std=c++14 a.cpp
I've just encountered an problem today: The following code seems to work in MSVC++ 2010 but not with Clang LLVM 4.1 (with GNU++11).
#include <fstream>
void foo(std::fstream& file){
file << "foo";
}
int main() {
std::fstream dummy("dummy");
foo(dummy);
return 0;
}
generates
Invalid operands to binary expression (std::fstream (aka basic_fstream<char>) and const char[4])
on Clang. I thought passing iostream arguments by reference would be common practice in C++. I'm not even sure if this is related to clang, C++11 or anything else.
Any idea how I can pass streams to functions then?
I assume that your original code (that you only partially posted in your original question) looked something like this:
#include <iosfwd>
void foo(std::fstream& file){
file << "foo";
}
int main() {
std::fstream dummy("dummy");
foo(dummy);
return 0;
}
Indeed, this gives the following error message with clang++ 3.2
Compilation finished with errors:
source.cpp:4:10: error: invalid operands to binary expression ('std::fstream' (aka 'basic_fstream<char>') and 'const char [4]')
file << "foo";
~~~~ ^ ~~~~~
source.cpp:8:17: error: implicit instantiation of undefined template 'std::basic_fstream<char, std::char_traits<char> >'
std::fstream dummy("dummy");
^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/iosfwd:118:11: note: template is declared here
class basic_fstream;
^
2 errors generated.
Unfortunately, you only posted the first error message but not the second.
From the second error message, it is obvious that you only #include <iosfwd> but not #include <fstream>. If you fix that, everything will be OK.
Please post both the complete code and all the error messages next time.
I am completely new to C++ and I am now following the C++ Primer book.
I wrote a small example about strings, here is the code:
#include <iostream>
#include <string>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string s("Hello World");
for (auto &c : s)
c = toupper(c);
cout << s << endl;
return 0;
}
I am on Linux with GCC version 4.4.6 and I tried to compile this code with:
g++ test_strings.c -std=c++0x
but got the following errors:
test_strings.c: In function 'int main()':
test_strings.c:14: error: expected initializer before ':' token
test_strings.c:19: error: expected primary-expression before 'return'
test_strings.c:19: error: expected ')' before 'return'
I copied the program from the textbook, so I though it was a misspelling but after a check and trying searching on the web and updating my gcc the error reminds. Help will be greatly appreciated, thanks in advance.
As per the C++0x/C++11 Support in GCC page, you need to be running gcc 4.6 to get the range-for feature.
The 4.6 changes page contains:
Improved experimental support for the upcoming C++0x ISO C++ standard, including support for constexpr (thanks to Gabriel Dos Reis and Jason Merrill), nullptr (thanks to Magnus Fromreide), noexcept, unrestricted unions, range-based for loops (thanks to Rodrigo Rivas Costa), opaque enum declarations (thanks also to Rodrigo), implicitly deleted functions and implicit move constructors.
Since you're running gcc 4.4.6, it's not available to you.
When I compile this code:
template<typename T>
struct S {
std::vector<T> v;
S(initializer_list<T> l) : v(l) {
std::cout << "constructed with a " << l.size() << "-element list\n";
}
};
using the following command line:
clang++ -std=c++11 -stdlib=libc++ initializer_list.cpp
I get the following error.
initializer_list.cpp:12:23: error: expected ')'
S(initializer_list<T> l) : v(l) {
Does anyone know the fix if any??
Thanks in advance
You probably meant to write std::initializer_list<T>. Make sure you include <initializer_list>.
Your code sample is incomplete. It would be useful if you can provide a complete example. The problem with the code as written is that you're missing
#include <initializer_list>
#include <vector>
#include <iostream>
... and initializer_list is in namespace std, so you're also missing a std:: from your constructor declaration.
However, since you've claimed that neither of these is the issue, the most likely cause would seem to be that your C++ standard library implementation doesn't provide std::initializer_list. That would be the case if Clang is using GCC's libstdc++, and you do not have a suitably new version of that installed: you need at least version 4.4, but note that a patch is required to fix bugs in libstdc++-4.4 in order to make it work with Clang in C++11 mode, otherwise you will get errors about type_info and various other problems.
Also, you say that the diagnostic you received is this:
initializer_list.cpp:12:23: error: expected ')'
S(initializer_list<T> l) : v(l) {
^
(I've reconstructed the caret from the provided column number; it would be useful to preserve it in future questions.) For any of the above explanations, this will not be the first diagnostic which Clang produces; that would be something along the lines of:
initializer_list.cpp:12:5: error: no template named 'initializer_list'; did you mean 'std::initializer_list'?
S(initializer_list<T> l) : v(l) {
^~~~~~~~~~~~~~~~
std::initializer_list
So either you've missed out the first diagnostic from your question, or the problem is that you have declared some other (non-template) type named initializer_list in the code you omitted in your question, and that is hiding std::initializer_list. Without seeing the rest of your code or the rest of your diagnostics, it's not possible to tell which.