I compiled this code using g++ and got an error:
#include<iostream>
using namespace std;
int main()
{
int j{ 0 };
cout << "j = " << j << endl;
return 0;
}
This is the error:
error: expected ';' at end of declaration
int j{ 0 };
^
;
1 error generated.
You have probably used an old version of compiler .
On Godbolt, I have checked that it may be older than 4.4.7.
You may have to add -std=c++11 flag to compile.
Newer compiler has this standard enable as default.
Related
I'm trying to compile the following C++ code on Visual Studio Code, using the Mac clang compiler.
#include <iostream>
int main() {
int x { 5 };
std::cout << x;
return 0;
}
However, this returns an error, on the line of the list initialization: int x{ 5 };. Specifically, it says I need to insert a semicolon after the x.
I don't get what's wrong with this code, it works fine on an online compiler. How do I fix this?
Running man clang in the Terminal and skimming through, I found this:
The default C++ language standard is gnu++14.
UPDATE: I ran clang++ main.cpp in the compiler and it returned that semicolon error. This isn't a problem with VSCode, so I'll remove that tag.
Here's the error:
main.cpp:3:10: error: expected ';' at end of declaration
int x { 5 };
^
;
1 error generated.
I'm getting an unexpectred error when I initialize a vector in the main.
I was expecting the following output:
0 1 2
I can't see why it's not working. I also writed the same code in another pc using the same compiler, and it works.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vett = {0,1,2};
for (int i : vett) {
cout << i << " ";
}
return 0;
}
error: could not convert '{0, 1, 2}' from '<brace-enclosed initializer list>' to 'std::vector<int>'|
You need to compile with at least C++11. List initialization came with C++11.
-std=c++11
You are compiling with something older than C++11, it does not support initializer list constructor.
If you are using Code::Blocks follow these steps:
Settings -> compiler -> compiler flags -> select C++11 or above
#include <ios>
#include <iostream>
#include <map>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
map<int, int> v;
int i;
int t;
while (cin >> i) {
v[i] = t++;
}
auto mi = i;
auto mt = t;
for (const auto p : v) {
if (p.second < mt) {
mi = p.first;
mt = p.second;
}
}
cout << mi << '\n';
return 0;
}
The abovementioned program makes heavy use of an uninitialized variable t, but GCC does not report it with -Wall or -Wuninitialized. Why is it so?
It is worth noting that Clang catches it:
main.cpp:13:12: warning: variable 't' is uninitialized when used here [-Wuninitialized]
v[i] = t++;
^
Used g++ (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2).
Used clang version 4.0.1 (tags/RELEASE_401/final).
As you can see in https://godbolt.org/g/kmYMC1 GCC 7.2 does not report it even when it should. I will create a ticket in GCC's issue tracker.
g++'s warning flag is not called -Wuninitialized: it is called -Wmaybe-uninitialized.
Also, as Jonathan Wakely noted in his answer, g++ is able to detect usage of uninitialized variables only when optimizations are enabled.
Enabling both -Wmaybe-initalized and optimizations produces the expected warning: https://godbolt.org/g/3CZ6kT
Note that -Wmaybe-initalized is enabled by default with both -Wall and -Wextra.
GCC can only detect uninitialized variables when optimization is enabled, because the logic for tracking the values of variables is part of the optimization machinery.
If you compile with -O -Wall you get a warning:
<source>: In function 'int main()':
12 : <source>:12:13: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
v[i] = t++;
~^~
Compiler exited with result code 0
https://godbolt.org/g/327bsi
Take the following code
#include <iostream>
template<typename T>
T f(T x, unsigned y) {
if (y < 0) return x;
return static_cast<T>(0);
}
using namespace std;
int main() {
int a = f(2, 3);
std::cout << a << std::endl;
return 0;
}
where function f clearly always returns 0. Compiling it with g++-7.2.0 -Wall -Wextra gives no hint about pointless comparison. However, clang warns us nicely:
a.cpp:7:11: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (y < 0) return x;
~ ^ ~
1 warning generated.
Why is this so (I presume templates are the root of the problem) and can gcc be forced to output the warning in this case?
This is a regression bug in some versions of GCC (including 8.x and 9.x - which are still the default compilers on many distributions at the time of writing).
The bug was tracked here (#jureslak file it again, but that was marked as dupe) and has been resolved. See the warning with GCC 10.1 (Godbolt).
I am compiling a code, but I seem to get an error, even as I examine my code, I find it weird that I'm getting this error.
My code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "2A";
int n = stoul(s, nullptr, 16);
cout << n << endl;
return 0;
}
and in the command line, I'm typing g++ -std=c++11 main.cpp, I'm using -std=c++11 because the function stoul is from C++11 according to this page stoul.
The error I'm getting is the following:
main.cpp: In function 'int main()':
main.cpp:8:30: error: 'stoul' was not declared in this scope
int n = stoul(s, nullptr, 16);
^
Why am I getting this error?