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

Discussion in 'C++' started by Single Stage to Orbit, Jul 28, 2012.

  1. 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
     
    Single Stage to Orbit, Jul 28, 2012
    #1
    1. Advertising

  2. Single Stage to Orbit

    qwak Guest

    qwak, Jul 28, 2012
    #2
    1. Advertising

  3. 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
     
    Single Stage to Orbit, Jul 28, 2012
    #3
  4. Single Stage to Orbit

    Rui Maciel Guest

    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, Jul 28, 2012
    #4
  5. Single Stage to Orbit

    Rui Maciel Guest

    Rui Maciel, Jul 28, 2012
    #5
  6. 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
     
    Single Stage to Orbit, Jul 28, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    22
    Views:
    1,138
    Tim Roberts
    Mar 21, 2006
  2. Xah Lee
    Replies:
    23
    Views:
    1,084
    Tim Roberts
    Mar 21, 2006
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    cannot compile the following example code (person-pointer)

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Mar 25, 2006, in forum: C++
    Replies:
    26
    Views:
    657
    Alex Buell
    Mar 31, 2006
  4. fAnSKyer
    Replies:
    2
    Views:
    534
    Alf P. Steinbach
    Jun 7, 2009
  5. Xah Lee
    Replies:
    21
    Views:
    802
    Tim Roberts
    Mar 21, 2006
Loading...

Share This Page