Error on vector of objects containing threaded function

Hello,
Im having a difficult problem - at least for me, at least from the information the compiler spit out.

I wrote a custom class which inherits ofThread on public, basic usage:

  
  
class MyClass: public ofThread  
{  
     int A, B, C;  
     void threadedFunction();  
     MyClass();  
}  
  

… if I instantiate the class (run its methods, including the threaded function) no problem.

I then created a vector of this class:

  
  
// in main function  
...  
vector<MyClass> myList;  

… and here lies the problem: as soon as I try to add an instance of the class to the vector, the compiler fails saying that it cant access privte members in class Poco:FastMutex [sup](utils\ofthread.h(190): error C2248: ‘Poco::FastMutex::FastMutex’ : cannot access private member declared in class ‘Poco::FastMutex’)[/sup] and Poco:Thread [sup](utils\ofthread.h(190): error C2248: ‘Poco::Thread::Thread’ : cannot access private member declared in class ‘Poco::Thread’)[/sup]

  
  
// in main function  
...  
myList.push_back(MyClass());    //ERROR!  

  
  
// in main function  
...  
MyClass temp;              //OK SO FAR  
myList.push_back(temp);    //ERROR!  

Do you know of any method to create a vector of objects containing a threaded function?

I am writing an application with many objects (belonging to the same class) having a quite complex data structure which needs to be analysed in parallel: while the main application thread takes care of interacting with the user and rendering the scene, each object gets its data analysed in parallel on separate threads.

Any help welcome!

You could create a vector of pointers to your class:

  
vector<MyClass*> myList;   

That should give you no problem, just use “new” to instantiate and add elements to your vector.

  
myList.push_back(new MyClass());   

I’m guessing this is due assignment to the vector position, when not using pointers we’re probably copying private attributes (which can’t be done), by using pointers we instantiate directly into the vector, no forbidden copies are performed.

1 Like

cheers! that worked.

make sure you delete them before calling clear() as well

To anyone looking at this, the reason the OP’s initial code didn’t work is probably because he forgot the public keyword. Which makes MyClass's constructor private!

class MyClass: public ofThread  
{  
public: // <- this one
     int A, B, C;  
     void threadedFunction();  
     MyClass();  
}  

Using raw pointers will give you other kind of trouble that you probably want to avoid. :wink: