In the following example:
void foo (double *ptr)
{
const double * restrict const restr_ptr=ptr;
}
I get this error:
error: expected a ";" const double * restrict const restr_ptr=ptr;
^
I compile with -std=c99, using gcc 3.4
Any Ideas?
In C++, restrict is not a keyword (except for Microsoft extensions). It doesn't mean what it does in C. It looks as though you tried to apply C99 mode to your C++ compiler. Use a C compiler to compile C code, and use a C++ compiler to compile C++. Neither language is a subset of the other.
Related
There seems to be a problem, using the literal i in C++ with std::complex.
Consider the following code:
std::complex<double> a = -1.0i * 42.0;
std::complex<double> b = a + 1.0i;
The second line fails to compile with:
error: no match for ‘operator+’ (operand types are ‘std::complex<double>’ and ‘__complex__ double’)
This also shows up when using the complex literal in function calls, e.g.
std::exp<std::complex<double>>( 1.0i * 3.14159 );
How come the complex literal 1.0i is not convertible to std::complex<double>?
Do I have to explicitly construct a std::complex with 1.0i?
You should recompile with --std=c++14 (no GNU ext) to avoid conflict of i suffix with gcc extension
The ISO C++14 library also defines the ‘i’ suffix, so C++14 code that includes the <complex> header cannot use ‘i’ for the GNU extension. The ‘j’ suffix still has the GNU meaning.
I tried to get myself into C++ and purchased the book "Programming - Principles and Practice Using C++" by Bjarne Stroustrup.
When I tried to get compile the following source code:
#include "std_lib_facilities.h"
int main(){
cout<<"Hello, World!\n";
keep_window_open();
return 0;
}
I am getting following compile error:
In file included from /Users/hypertrooper/Documents/Programming - Principles and Practice Using C++/hello_world.cpp:1:
std_lib_facilities.h:71:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = typename std::vector<T>::size_type;
^
std_lib_facilities.h:102:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using size_type = std::string::size_type;
^
std_lib_facilities.h:107:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:113:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:213:107: error: expected '(' for function-style cast or type construction
inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
~~~~~~~~~~~~~~~~~~~~~~~~~~^
std_lib_facilities.h:222:20: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Value_type = typename C::value_type;
^
std_lib_facilities.h:225:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Iterator = typename C::iterator;
^
6 warnings and 1 error generated.
I get it that my compiler is not using C++11 feature, but I do not know how I can update the compiler. I should let know that I am using a MacOSX (10.10 Yosemite) and tried to compile with xCode and textmate. I even tried to follow this tutorial(https://wiki.helsinki.fi/display/HUGG/Installing+the+GNU+compilers+on+Mac+OS+X), but it did not helped. (At least when I tried to compile with text mate)
I hope you are able to help me. :(
If you are on a Mac or Linux, the compiler is usually g++ or clang; to access C++11, just specify -std=c++11 as an option when invoking the compiler (Assuming that you have an up-to-date version).
What you need to do is open the project settings -> Build Settings and set C++ Language Dialect to C++11 and the C++ Standard Library to libc++ (LLVM C++ standard library with C++11 support).
From your build settings, adjust the C++ Language Dialect to C++11...
Also make sure you are using the LLVM C++ library, as it has full C++ 11 support.
I am trying to use constants in a class which should be okay in c++11 but I get this warning:
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
The problem is that I get this many (more than 10) times for every constant I declare. This effectively fills the build messages and makes any other compiler warnings impossible to find.
I would like to make it so this no longer shows up in my build messages box.
I know people like to see relevant code so here it is:
class GameState: public State
{
public:
const Uint8 * keyStates;
Point gameMousePos;
int UIType;
std::vector<UI *> UIs;
Texture * lockingTex;
HitBox * inGame;
const int buttonDim = 100;
const int buttonOffY = 70;//distance from bottom
const int buttonOffX = 130;//distance from each other
const int buttonTextOffY = 140;//text distance from bottom
bool locking;
bool noPlaceBool;
float gameSpaceScale;
HitBox * gameSpace;
Texture * bkg;
float windowRotSpeed;
float inHandRotSpeed;
float windowMoveSpeed;
GameState();
void handle_events();
void logic();
void render();
void save();
void load_save();
}
The compiler warning tells you exactly what to do:
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
By default g++ uses C++03.
To activate C++11 features you need to tell the compiler to use C++11
g++ -std=c++11 stuff.cpp
Once you are using C++11 the default features ("enabled by default") for that language will be enabled.
The compiler is telling you that it should be static:
warning: non-static data member ...
So if you were to add the static keyword, it would solve your problem.
So change these:
const int buttonDim = 100;
With:
static const int buttonDim = 100;
And the warnings should go.
Note that was part of C++ for a very long time (only cl [Microsoft compiler] did not support it well before 2008 or so.)
As a side note, a good programmer wants to do the opposite: transform all warnings to errors to be forced to fix all warnings. I very rarely have to go around a warning and it is always rather edgy cases (like compare two floating point numbers with == or !=. So all of that to say, I would actually advice you use -Werror and always find the exact reason for a warning.
Of course, if you are working with someone else code... that's a different story. They may not want to fix their code.
When the following bit of code is compiled with the diab c++ compiler (dplusplus), it generates a conversion warning on the third line. It can be resolved by casting the result of the (&&) operator to anything other than bool.
Code:
bool first = 1;
bool second = 1;
bool ret = (first && second); //generates compile warning
Error:
warning: (etoa:1643): narrowing or signed-to-unsigned type conversion
found: int to unsigned char
I verified that nothing is defining bool to another type. Does this look like a compiler issue, or is there something else I might be missing that could cause this?
Wind River's web site indicates that the Diab compiler can compile either C or C++.
In C, the && operator yields a result of type int, with the value 0 or 1. That's consistent with the warning you're seeing.
As of the 1990 ISO standard, C did not have a built-in bool type. It was common to define bool as a typedef. It appears from the message that bool is a typedef for unsigned char, probably in some header. The 1999 ISO C standard adds a new predefined boolean type called _Bool; the identifier bool is defined in <stdbool.h> as a macro that expands to _Bool. But if <stdbool.h> isn't included, bool can be defined in some other way.
In C++, && yields a result of type bool with the value false or true, and bool is a distinct fundamental type. This has been the case at least since the 1998 ISO C++ standard.
I strongly suspect you're getting this warning because you're compiling your code as C rather than as C++. A less likely possibility is that the Diab compiler doesn't fully conform to the C++ standard; it might have a way to tell it to conform more closely.
I haven't used the Diab compiler. Typically you can control the language being compiled by using a particular file extension (typically .c for C and .cpp for C++), or by using a different command, or both.
Consult the compiler's documentation to find out how to invoke it as a conforming C++ compiler.
As an experiment, before changing the way you invoke the compiler, you might try adding a declaration:
int class;
to your source file. This is legal in C, but a syntax error in C++ (since class is a C++ keyword).
UPDATE:
The OP says he's definitely compiling as C++, not as C. But the warning message implies that && yields int, and that bool is the same type as unsigned char.
A warning doesn't directly indicate that the compiler is not conforming; compilers can warn about anything they like. But the content of this warning does suggest a compiler bug, or at least a compiler that doesn't conform to any C+
Any conforming C++ compiler must produce diagnostics for this program. What does your compiler do? (Please don't add any #include directives.)
int main() {
class dummy { }; // Just to make sure it's C++
bool b;
unsigned char c;
bool* pb = &c; // Invalid conversion
unsigned char* pc = &b; // Invalid conversion
}
And what output do you get when you compile and execute this program?
#include <iostream>
int main() {
std::cout << "__cplusplus = " << __cplusplus << "\n";
}
When I compile this helloworld example, I get the following error repeated 4 times:
error: expected primary-expression before ‘.’ token
Here is the code:
static struct fuse_operations hello_oper = {
.getattr = hello_getattr,
.readdir = hello_readdir,
.open = hello_open,
.read = hello_read,
};
int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &hello_oper);
}
Your compiler is too old. It needs to support C99. Pass in -std=c99 if the compiler is current enough
That syntax is using a new feature of the C99 language standard called designated initializers. That feature is not part of the more common C89 standard (aka ANSI C), so a C89 compiler will give you syntax errors when you try to compile code that uses it.
To fix it, tell your compiler to use its C99 mode if it has one. For example, if you're using GCC, you should pass the -std=c99 compiler option. If your compiler doesn't support C99 at all, you'll have to either switch to a compiler that does, or refactor the code to avoid using C99 features.
Actually, gcc support newer dialects of C (or of C++). Try passing it gcc -std=c99 -Wall
Ran into a similar error, although I was unable to overcome it by adding "#define FUSE_USE_VERSION ..." as mentioned in one of the comments above.
To overcome the error, I wrote a wrapper around the fuse_operations as follows:
struct my_operations: public fuse_operations {
my_operations()
{
read = my_read;
open = my_open;
}
} operations;
main(int argc, char* argv[])
{
return fuse_main(argc, argv, &operations, NULL);
}