Umable to compile the following threads example with G++ 4.5.3,any ideas how to fix?

  • Thread starter Single Stage to Orbit
  • Start date
S

Single Stage to Orbit

I get the following error when trying to compile with GNU G++ 4.5.3, how
can I change it to get it to build?

$ g++ -std=gnu++0x threads1.cpp -o threads1
threads1.cpp: In function ‘int main()’:
threads1.cpp:18:57: error: expected ‘)’ before ‘;’ token
threads1.cpp:21:15: error: expected initializer before ‘:’ token
threads1.cpp:27:2: error: expected primary-expression before ‘return’
threads1.cpp:27:2: error: expected ‘)’ before ‘return’

#include <iostream>
#include <vector>
#include <thread>
#include <functional>

void func(int& counter)
{
for (int i = 0; i < 10000; ++i)
++counter;
}

int main()
{
int counter = 0;
std::vector<std::thread> threads;
for (int i = 0; i < 10; ++i)
{
threads.push_back(std::thread{func, std::ref(counter)};
}

for (auto& t : threads)
{
t.join();
}

std::cout << "Result = " << counter << '\n';
return 0;
}
 
S

Single Stage to Orbit

W dniu 28.07.2012 02:35, Single Stage to Orbit pisze:

Ah, a missing ) bracket. Now that I've corrected it, I now get the following error:

$ g++ -std=gnu++0x threads1.cpp -o threads1
threads1.cpp: In function ‘int main()’:
threads1.cpp:21:15: error: expected initializer before ‘:’ token
threads1.cpp:27:2: error: expected primary-expression before ‘return’
threads1.cpp:27:2: error: expected ‘)’ before ‘return’

That corresponds to the line:
for (auto& t : threads)

But that looks OK?

Thanks for pointing out the missing bracket though.
 
R

Rui Maciel

Single said:
Ah, a missing ) bracket. Now that I've corrected it, I now get the
following error:

$ g++ -std=gnu++0x threads1.cpp -o threads1
threads1.cpp: In function ‘int main()’:
threads1.cpp:21:15: error: expected initializer before ‘:’ token
threads1.cpp:27:2: error: expected primary-expression before ‘return’
threads1.cpp:27:2: error: expected ‘)’ before ‘return’

That corresponds to the line:
for (auto& t : threads)

But that looks OK?

Thanks for pointing out the missing bracket though.

I believe that support for range-based for loops was only added to GCC in
v4.6. If you replace that range-based for() loop with an old-fashioned one
then that error will be taken care of.


Rui Maciel
 
S

Single Stage to Orbit

I believe that support for range-based for loops was only added to GCC
in
v4.6. If you replace that range-based for() loop with an
old-fashioned one then that error will be taken care of.

Thanks, I'll upgrade my compiler soon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top