Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Umable to compile the following threads example with G++ 4.5.3,any ideas how to fix? (http://www.velocityreviews.com/forums/t948627-umable-to-compile-the-following-threads-example-with-g-4-5-3-any-ideas-how-to-fix.html)

Single Stage to Orbit 07-28-2012 12:35 AM

Umable to compile the following threads example with G++ 4.5.3,any ideas how to fix?
 
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;
}

--
Tactical Nuclear Kittens


qwak 07-28-2012 12:51 AM

Re: Umable to compile the following threads example with G++ 4.5.3,any ideas how to fix?
 
W dniu 28.07.2012 02:35, Single Stage to Orbit pisze:

> threads1.cpp:18:57: error: expected ‘)’ before ‘;’ token


--
Piotr Beling - http://qwak.w8.pl http://warcaby.w8.pl http://bcalc.w8.pl
http://kiedygdzie.co



Single Stage to Orbit 07-28-2012 12:55 AM

Re: Umable to compile the following threads example with G++4.5.3, any ideas how to fix?
 
On Sat, 2012-07-28 at 02:51 +0200, qwak wrote:
> W dniu 28.07.2012 02:35, Single Stage to Orbit pisze:
>
> > threads1.cpp:18:57: error: expected ‘)’ before ‘;’ token


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.
--
Tactical Nuclear Kittens


Rui Maciel 07-28-2012 07:46 AM

Re: Umable to compile the following threads example with G++ 4.5.3, any ideas how to fix?
 
Single Stage to Orbit wrote:

> 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

Rui Maciel 07-28-2012 07:50 AM

Re: Umable to compile the following threads example with G++ 4.5.3, any ideas how to fix?
 
Rui Maciel wrote:

> I believe that support for range-based for loops was only added to GCC in
> v4.6.


It was.

http://gcc.gnu.org/gcc-4.6/changes.html


Here is a list of what C++11 features were implemented in GCC.

http://gcc.gnu.org/projects/cxx0x.html


Rui Maciel

Single Stage to Orbit 07-28-2012 08:37 AM

Re: Umable to compile the following threads example with G++4.5.3, any ideas how to fix?
 
On Sat, 2012-07-28 at 08:46 +0100, Rui Maciel wrote:
> > 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.


Thanks, I'll upgrade my compiler soon.
--
Tactical Nuclear Kittens



All times are GMT. The time now is 10:47 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.