I've been messing with some vectors (the STL ones). In some other project I couldn't pass the vector of object types memeory adress around? Anyways why doesn't this code work?
//DEFAULT
#include <iostream>
#include <vector>
#include <thread>
usingnamespace std;
//CLASS BEGIN
class chickens
{
public:
chickens();
void set_chicken_size(int);
int get_chicken_size();
~chickens();
private:
int chicken_size;
};
chickens::chickens()
{
}
void chickens::set_chicken_size(int set_size)
{
this->chicken_size = set_size;
}
int chickens::get_chicken_size()
{
returnthis->chicken_size;
}
chickens::~chickens()
{
}
//CLASS END
//PROTOTYPE
void change_vectors(vector<chickens>*);
int main() {
vector<chickens> chicken_vector; //This is not an object, but a vector that takes the object type chicken
thread change_chickens_thread(&chicken_vector); //Testing pointer with thread (not that it should matter)
change_chickens_thread.join();//Sync
for (int i = 0; i < 5; i++) { //Output every vector element
std::cout << chicken_vector[i].get_chicken_size() << std::endl;
}
system("pause");
return 0;
}
void change_vectors(vector<chickens> *chicken_vector_ptr) {
chickens chicken[5]; //Create 5 chicken objects
for (int i = 0; i < 5; i++) {//Loop for assigning chicken sizes and then assigning those objects to the back of the vector
chicken[i].set_chicken_size(i);
chicken_vector_ptr->push_back(chicken[i]);//Vectors are dynamically sized right?
}
}
I should go to bed... I'd agree that its pretty amazing that it even compiles when attempting to create a thread with a vector adress as function... My bad.