Simple program won't compile with qtcreator - c++

I installed qt 4.7.4 and gcc 4.6.1. I tried to compile this program but it won't compile for me:
Why cannot I compile this code?
#include <QApplication>
#include <iostream>
using std::cout;
int main(int argc, char** argv)
{
QApplication app(argc,argv);
int a[] = {1,2};
for (auto e : a)
{
cout << e << '\n';
}
return app.exec();
}
Error:
C:...\main.cpp:9: error: 'e' does not name a type

for (auto e : a)
is a range based for loop from the c++11 standard. You need to enable the c++11 in gcc with the -std=c++0x command line.

For me this works (g++ 4.6.1, Qt 4.7.1):
g++ --std=c++0x -I$QTDIR/include/QtGui -I$QTDIR/include \
test.cpp -L$QTDIR/lib -lQtCore -lQtGui

You need --std=c++0x compiler flag.

My guess is that qtcreator (and qmake) does not feed the compiler with the flag instructing it to use C++2011.

First, you want to be sure that your C++ file is compiled with the C++11 dialect (ie using -std=c++0x flag to g++) since you use the auto type inference feature.
Then, I think that your for loop might not be valid. Perhaps you want a to be a std::vector<int>

Related

How To Run Google test from terminal on C code?

I am testing C code using googleTest.
My test.cpp file look like that
#include <gtest/gtest.h>
extern "C" {
#include "list.h"
#include "list.c"
}
TEST(ListTest, singleInsertion) {
// some tests
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
However trying to run the test from the terminal using
g++ test.cpp -lgtest gives Errors and warning as if the code being tested is C++ not C.
Error and warning Examples :
error: invalid conversion for mallocs and
warning: ISO C++ forbids converting a string constant to ‘char*'
how can I declare that my tested files are C not C++ ?
However trying to run the test from the terminal using g++ test.cpp -lgtest gives Errors and warning as if the code being tested is C++ not C.
That's because you are compiling it as C++ by using the g++ compiler. Use gcc to compile as C.
Unfortunately, this code won't compile as C - it'll choke on the google::InitGoogleTest() call because C doesn't recognize the :: scoping operator. I'm not familiar with this testing framework, but at first glance it looks like it's meant to be used with C++, not C.
The way to fix this is to remove the #include "list.c" directive
extern "C" {
#include "list.h"
}
and compile it separately as C:
gcc -c list.c
then compile your tester:
g++ -c test.cpp
and then link the object files with the library:
g++ -o test test.o list.o -lgtest

How to get the compiler to warn that this is an invalid bool?

We just got burnt by a typo: "constexpr bool maxDistance=10000;"
Both gcc and clang compile this with no warning.
The real error here is that the variable shouldn't have been of type bool, but should have been an integer type instead.
How can we ensure we get a compiler warning in future?
#include <iostream>
constexpr bool number = 1234;
int main(int argc, char* argv[])
{
std::cout << number + 10000 << std::endl; // prints 10001.
return number;
}
The error here is that the variable is declared with the wrong type, however neither clang nor gcc give a warning.
gcc -Wall -std=c++14 test.cpp -lstdc++
clang -Wall -std=c++14 test.cpp -lstdc++
(using gcc 5.4.0 and clang 3.8.0)
Note: I've since learnt about a possible compile flag: -Wint-in-bool-context however this doesn't appear to be implemented in the version I'm using (5.4.0) nor in clang (3.8.0).
Is this the right way to go?
You should use direct list initialization syntax, it prohibits narrowing:
constexpr bool number{1234}; // error: narrowing conversion of '1234' from 'int' to 'bool' [-Wnarrowing]
I've discovered that gcc has a flag '-Wint-in-bool-context' however this doesn't appear to be implemented in the version I'm using (5.4.0) nor in clang (3.8.0).
Is this the right way to go?

std::byte is not member of 'std'

I'm trying to learn new features/gimmicks of c++17, but then I got to std::byte and for some unknown reason I can't seem to be able to compile even most basic "hello world" type program with the type.
entire program:
#include <cstddef>
int main(int argc, char* argv[])
{
std::byte byte;
return 0;
}
compilation command:
g++ ./main.cpp
But the output is always:
./main.cpp: In function ‘int main(int, char**)’:
./main.cpp:4:10: error: ‘byte’ is not a member of ‘std’
std::byte byte;
I work on Ubuntu 18.04 with gcc 7.4.0. I have checked "/usr/include/c++/7.4.0/" and header file cstddef is there and byte seems to be defined.
I have also tried to use clang:
clang++ ./main.cpp
But the result was same. At this point I can only think that cstddef is corrupted/bugged. Are there any solutions to this?
As πάντα ῥεῖ pointed out in comment I was missing c++17 compile flag. Right compilation command:
g++ -std=c++17 ./main.cpp
If you use clang 5.0 (even with -std=c++17 flag) the same error occur.
In that case, to solve this you need to upgrade to clang 6.
A temporay and quick workaround is possible (but not recommanded since it plays with std namespace), it could be something like:
#if defined(__clang__) && __cplusplus >= 201703L && __clang_major__ < 6
// This is a minimal workaround for clang 5.0 with missing std::byte type
namespace std {
enum class byte : unsigned char {};
}
#endif

CUDA 6.5 with g++ does not support c++11?

I read it here that CUDA 6.5 has started support for C++11 :
https://groups.google.com/forum/#!topic/thrust-users/R37GIkMG4tk
But when I compile an example code below, I got
$ nvcc -std=c++11 cu-gcc11.cu -o test
nvcc warning : The -c++11 flag is not supported with the configured host compiler. Flag will be ignored.
cu-gcc11.cu(7): error: explicit type is missing ("int" assumed)
My setting : CUDA 6.5, g++ 4.5, ubuntu 12.04
Codes :
#include <cuda.h>
#include <iostream>
__host__ void test() {
float a = 12.;
double b = 3.;
auto c = a * b;
std::cout << c << std::endl;
}
int main()
{
test();
return 0;
}
C++11 support in nvcc is experimental at this time. In order to properly use it you will need an appropriate configuration. This is not documented anywhere AFAIK, but you should have good results with either Fedora 20 or Ubuntu 14.04, both of which are supported configs for cuda 6.5 and include GCC 4.8.x.
In your case your GCC version is just too old.
I don't think -std=c++11 was available in GCC 4.5. Try -std=c++0x.

"to_string" isn't a member of "std"?

Okay, so I have
tmp.cpp:
#include <string>
int main()
{
std::to_string(0);
return 0;
}
But when I try to compile I get:
$ g++ tmp.cpp -o tmp
tmp.cpp: In function ‘int main()’:
tmp.cpp:5:5: error: ‘to_string’ is not a member of ‘std’
std::to_string(0);
^
I'm running g++ version 4.8.1. Unlike all the other references to this error that I found out there, I am not using MinGW, I'm on Linux (3.11.2).
Any ideas why this is happening? Is this standard behaviour and I did something wrong or is there a bug somewhere?
you may want to specify the C++ version with
g++ -std=c++11 tmp.cpp -o tmp
I don't have gcc 4.8.1 at hand , but in older versions of GCC,
you can use
g++ -std=c++0x tmp.cpp -o tmp
At least gcc 4.9.2 I believe also support part of C++14 by specifying
g++ -std=c++1y tmp.cpp -o tmp
Update:
gcc 5.3.0 (I am using the cygwin version) supports both -std=c++14 and -std=c++17 now.
to_string works with the latest C++ versions like version 11. For older versions you can try using this function
#include <string>
#include <sstream>
template <typename T>
std::string ToString(T val)
{
std::stringstream stream;
stream << val;
return stream.str();
}
By adding a template you can use any data type too.
You have to include #include<sstream> here.