I was experimenting with some of the new C++0x features with G++. Lambdas, auto, and the other new features worked like a charm, but the range-based for-loop failed to compile. This is the program I was testing:
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> data = { 1, 2, 3, 4 };
for ( int datum : data )
{
std::cout << datum << std::endl;
}
}
I compiled it with:
g++ test.cpp -std=c++0x
I also tried gnu++0x, but the output was the same.
This was the output:
test.cpp: In function ‘int main()’:
test.cpp:8:21: error: expected initializer before ‘:’ token
test.cpp:12:1: error: expected primary-expression before ‘}’ token
test.cpp:12:1: error: expected ‘;’ before ‘}’ token
test.cpp:12:1: error: expected primary-expression before ‘}’ token
test.cpp:12:1: error: expected ‘)’ before ‘}’ token
test.cpp:12:1: error: expected primary-expression before ‘}’ token
test.cpp:12:1: error: expected ‘;’ before ‘}’ token
Thanks in advance for your help.
EDIT: I am using GCC version 4.5.2, which I now see is too old.
You need GCC 4.6 and above to get range-based for loops.
GCC's C++0x status
$ cat for.cpp
#include <iostream>
int main()
{
for (char c: "Hello, world!")
std::cout << c;
std::cout << std::endl;
return 0;
}
$ g++ -std=c++0x -o for for.cpp
$ ./for
Hello, world!
$ g++ --version
g++ (GCC) 4.6.1 20110325 (prerelease)
Related
When I'm trying to run a simple hello world script it sends this error message, Can anyone help me solving this issue?
[Running] cd "c:\Users\NickT\OneDrive\Documents\C++ Tutorial\" && g++ helloworld.cpp -o helloworld && "c:\Users\NickT\OneDrive\Documents\C++ Tutorial\"helloworld
helloworld.cpp: In function `int main()':
helloworld.cpp:9: error: expected primary-expression before "msg"
helloworld.cpp:9: error: expected `;' before "msg"
helloworld.cpp:11: error: expected primary-expression before "const"
helloworld.cpp:11: error: expected `;' before "const"
helloworld.cpp:16: error: expected primary-expression before '}' token
helloworld.cpp:16: error: expected `)' before '}' token
helloworld.cpp:16: error: expected primary-expression before '}' token
helloworld.cpp:16: error: expected `;' before '}' token
[Done] exited with code=1 in 0.233 seconds
This is the written code
#include <iostream>
int main(){
std::cout << "Hello World" << std::endl;
return 0;
}
I revised the code you have posted as well as added a piece of code to utilize the namespace std. Analyzing the output from your compiler it does seem that you may have not added a semi colon. Alternatively, it's not necessary to reference the std library when calling it's functions but it's up to your preference. It doesn't seem though to be an issue with your code either. Can you post the actual code you've attempted to compile because there doesn't seem to be an issue with this piece.
Revised Code:
#include <iostream>
using namespace std;
int main() {
std::cout << "Hello World" << std::endl;
return 0;
}
In C++, the following is valid and I can run it without a problem:
int main(){
if (int i=5)
std::cout << i << std::endl;
return 0;
}
However, even though the following should also be valid, it gives me an error:
if ((int i=5) == 5)
std::cout << i << std::endl;
Error:
test.cpp: In function ‘int main()’:
test.cpp:4:10: error: expected primary-expression before ‘int’
if ((int i=5) == 5)
^
test.cpp:4:10: error: expected ‘)’ before ‘int’
test.cpp:5:36: error: expected ‘)’ before ‘;’ token
std::cout << i << std::endl;
^
Furthermore, in C++17 the below code must be valid too, but it gives me a similar error again:
if (int i=5; i == 5)
std::cout << i << std::endl;
Error:
test.cpp: In function ‘int main()’:
test.cpp:4:16: error: expected ‘)’ before ‘;’ token
if (int i=5; i == 5)
^
test.cpp:4:18: error: ‘i’ was not declared in this scope
if (int i=5; i == 5)
^
I am trying to compile with g++ test.cpp -std=c++17. g++ --version gives me g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609. What am I missing here?
if ((int i=5) == 5) is a syntax error. It does not match any supported syntax for if statements. The syntax is init-statement(optional) condition, where condition could either be an expression, or a declaration with initializer. You can read more detail about the syntax on cppreference.
if (int i=5; i == 5) is correct. However, you are using an old version of GCC that dates from before C++17 was standardized. You would need to upgrade your compiler version. According to C++ Standards Support in GCC this feature was added in GCC 7.
For starters, I believe your compiler is right to reject
if ((int i=5) == 5)
because this is not legal C++ code. A variable declaration statement isn’t an expression, so you can’t treat (int i = 5) as an expression.
For the second one, I suspect you just need to update your compiler. g++ 5.6 is a fairly old version at this point, and I believe more updates versions of g++ will handle that code with no problem.
When I run the following code on ubuntu(gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4)):
#include<iostream>
#include<vector>
#include<list>
using namespace std;
int main(){
vector <int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
list<int> temp;
for(auto i:v){
cout<<i<<" ";
temp.push_back(i);
}
for(auto i:temp){
cout<<i<<" ";
}
}
I get the following errors:
try.cpp: In function ‘int main()’:
try.cpp:13:10: error: ‘i’ does not name a type
for(auto i:v){
^
try.cpp:17:1: error: expected ‘;’ before ‘for’
for(auto i:temp){
^
try.cpp:17:1: error: expected primary-expression before ‘for’
try.cpp:17:1: error: expected ‘;’ before ‘for’
try.cpp:17:1: error: expected primary-expression before ‘for’
try.cpp:17:1: error: expected ‘)’ before ‘for’
try.cpp:17:10: error: ‘i’ does not name a type
for(auto i:temp){
^
try.cpp:20:1: error: expected ‘;’ before ‘}’ token
}
^
try.cpp:20:1: error: expected primary-expression before ‘}’ token
try.cpp:20:1: error: expected ‘;’ before ‘}’ token
try.cpp:20:1: error: expected primary-expression before ‘}’ token
try.cpp:20:1: error: expected ‘)’ before ‘}’ token
try.cpp:20:1: error: expected primary-expression before ‘}’ token
But when I run the code on online ide I works fine.
What is the problem with the code?
The link for code on online ide:No errors
Your code uses some of the C++11 features such as range based loops and auto specifier but you don't compile for the C++11 standard. You need to enable the C++11 support by including the -std=c++11 flag when compiling:
g++ -std=c++11 -o try try.cpp
The online compiler has this enabled by using the -std=gnu++1z flag.
This question is related to the one discussed here.
I try to use an initializer list to create an argument to be passed to operator[].
#include <string>
#include <vector>
struct A {
std::string& operator[](std::vector<std::string> vec)
{
return vec.front();
}
};
int main()
{
// ok
std::vector<std::string> vec {"hello", "world", "test"};
A a;
// error: could not convert '{"hello", "world", "test"}' to 'std::vector...'
a[ {"hello", "world", "test"} ];
}
My Compiler (GCC 4.6.1) complains:
g++ -std=c++0x test.cpp
test.cpp: In function ‘int main()’:
test.cpp:20:8: error: expected primary-expression before ‘{’ token
test.cpp:20:8: error: expected ‘]’ before ‘{’ token
test.cpp:20:8: error: expected ‘;’ before ‘{’ token
test.cpp:20:35: error: expected primary-expression before ‘]’ token
test.cpp:20:35: error: expected ‘;’ before ‘]’ token
Should this be valid C++11?
Interestingly, when using operator() instead of operator[] it works.
Yes, it is valid C++11 and should work in any compliant compiler.
Please note that C++11 support in gcc is quite immature, and this code example will not compile in any 4.6 version, but only in 4.7 svn snapshot versions.
I am a LEDA-6.3 user.
I have an error when compiling this simple code:
#include <LEDA/core/d_array.h>
#include <iostream>
using namespace std;
main()
{
d_array<string,string> dic;
dic["hello"] = "hallo";
dic["world"] = "Welt";
dic["book"] = "Buch";
dic["key"] = "Schluessel";
string s;
forall_defined(s,dic) cout << s << " " << dic[s] << endl;
}
G++ Compiler:
g++ -I$LEDAROOT/incl -L$LEDAROOT d_array.cpp /usr/lib/LEDA/libleda.a -lX11 -lm -o d_array
The ERROR:
d_array.cpp: In function ‘int main()’:
d_array.cpp:8: error: ‘d_array’ was not declared in this scope
d_array.cpp:8: error: expected primary-expression before ‘,’ token
d_array.cpp:8: error: expected primary-expression before ‘>’ token
d_array.cpp:8: error: ‘dic’ was not declared in this scope
Please if there is a guide for LEDA-6.3 give me the link
You probably mean leda::d_array or are forgetting using namespace leda;