0

I am attempting to make a vector of pointers to boost thread objects. This vector then is a member of a class that is created on the heap from a pointer when the class constructor is called. It looks something like this.

 #ifndef NETWORKSYSTEM_H #define NETWORKSYSTEM_H #include "Network.h" #include "Misc.h" #include "Enumerators.h" #include < vector> #include < boost\thread.hpp> #include < boost\filesystem.hpp> #include < string> #include < iostream> class NetworkSystem { private: Status NetworkStatus; boost::filesystem3::path *ProjectPath; std::string ProjectName; //vector for pointers to networks std::vector< Network*> *M_Network; //Threading Components boost::thread *MainThread; std::vector< boost::thread *> *WorkerThreads; void MainThreadFunction(); void WorkerThreadFunction(); public: NetworkSystem(); ~NetworkSystem(); int SetWorkerThreads(int P_WorkerThreads, bool Wait); int TotalNetworks(); int WorkerThreads(); int PauseAtNetworksCompletion(bool Wait); int PauseAtGenerationsCompletion(bool Wait); }; #endif 
 // class constructor NetworkSystem::NetworkSystem() { ProjectPath = new boost::filesystem3::path(); M_Network = new std::vector< Network*>; WorkerThreads = new std::vector< boost::thread*>; NetworkStatus = NO_PROJECT_OPEN; MainThread = new boost::thread(&NetworkSystem::MainThreadFunction, this); }; 

Visual C++ 2010 gives me errors with the boost::thread pointer vector. It underlines WorkerThreads in the constructor and says that "expression must be a modifiable lvalue". I have no problems when doing the same thing with the M_Network vector. If you believe this approach to organizing my worker threads into a vector of pointers so I can initialize and manage them individually is bad, then I suppose I COULD use a thread group, but would like to get this method to work. Any help? Thanks.

7
  • 1
    "looks something like this" is not sufficient to say what you're doing wrong. post complete code. Commented Mar 13, 2011 at 23:08
  • Do you want me to post my entire class? (50 lines) Commented Mar 14, 2011 at 1:06
  • 1
    @JAKE6459: the best would be a minimal (but complete) version that still exhibits the problem, but 50 lines is OK. Commented Mar 14, 2011 at 1:09
  • @Alf P. Steinbach: I added the class (took out a lot of comments to make it shorter). Commented Mar 14, 2011 at 1:22
  • 1
    @Jake why allocate std::vector on the heap? Internally it will allocate its storage on the heap anyhow. Commented Mar 14, 2011 at 1:44

2 Answers 2

1

I got a similar error as yours:

class A { int B; public: A() { B = 0; } int B(); }; 

Advice: Don't name your member functions the same name as your member data.

Sign up to request clarification or add additional context in comments.

1 Comment

Is using the phrase "Face-Palm" appropriate for this situation? Thanks though.
0

I tried to reproduce your problem into a smaller example for others. It seems to compile for me using gcc 4.2.1 on Mac OS X 10.6

#include <vector> #include <boost/thread.hpp> class Foo { public: Foo() { WorkerThreads = new std::vector<boost::thread*>; } private: std::vector< boost::thread *> *WorkerThreads; }; int main() { const Foo foo; } 

compile & link

g++ -I/opt/local/include -L/opt/local/lib -Wl,-rpath,/opt/local/lib -lboost_thread-mt vector.cc 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.