tags:

views:

22

answers:

3

I have the following class definition and for some reason I cannot define the threadpool inside the class definition itself. It says: syntax error: identifier 'numberofpoolthreads' I tried directly defining it in the class, but it gives me same syntax error, does anyone know why this is?

#include "stdafx.h"
#include <boost/threadpool.hpp>
#include <boost/threadpool/pool.hpp>
#include <boost/threadpool/detail/pool_core.hpp>

typedef boost::threadpool::fifo_pool resolverpool;

class ResolverThreadPoolManager
{
public:
    ResolverThreadPoolManager::ResolverThreadPoolManager(int numberofpoolthreads);
    ResolverThreadPoolManager::~ResolverThreadPoolManager();

    resolverpool p(numberofpoolthreads);

private:
    int numberofpoolthreads;    
};
+1  A: 

This line: resolverpool p(numberofpoolthreads); isn't valid in a class definition. You need a member variable, which you then initialise in the constructor. e.g.:

class ResolverThreadPoolManager
{
public:
    explicit ResolverThreadPoolManager(int numberofpoolthreads);
    ...

private:
    const resolverpool p;
};

ResolverThreadPoolManager::ResolverThreadPoolManager(int numberofpoolthreads)
    : p(numberofpoolthreads)
    {}
Oli Charlesworth
normally you initialize the threadpool like this: boost::threadpool:fifo_pool pool(5), where 5 is the number of threadpool threads. I want this number to be held in a variable, so how would I do this then?
Tony
@Tony: See the updates to my answer.
Oli Charlesworth
A: 

In your line
resolverpool p(numberofpoolthreads);
the argument "numberofpoolthreads" is not a type, so this is a malformed declaration. Perhaps
resolverpool p(int numberofpoolthreads);
? I imagine, perhaps incorrectly, that your error message also hints on which line the error occurred, which can help narrow down where in a file an error lies. (Although, typically only indicating "error is on this or some previous line.")

Eric Towers
A: 

If you want the variable 'numberofpoolthreads' to be used as an initialization argument you're going to need to place it within the call to the constructor:

  ResolverThreadpoolManager:ResolverThreadpoolManager(int num) : resolverpool(num), numberofthread(num){}

And for the default constructor you'll have to come up with a default argument.

wheaties