How to make vector<Object*> objects won't be compile error as i can't include vector?
I think i get troll by the this assignment and have problem at this assignment, i ask my professor, he blue tick me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int main() {
const int WIDTH = 502,
HEIGHT = 502;
ObjectFactory of(WIDTH,HEIGHT,Object::VectorType(1,1));
vector<Object*> objects;
list<Object*> more_objects;
generate_n(back_inserter(objects),10,RandomObject(of));
generate_n(back_inserter(more_objects),10,RandomObject(of));
int width, height;
char *data = ObjectFactory::ReadFromBitmap(&width,&height,"cube.bmp");
if (data != 0)
objects.push_back(of.CreateBackground(data,width,height));
RenderObjects(objects);
for_each(objects.begin(),objects.end(),Kill());
RenderObjects(more_objects);
for_each(more_objects.begin(),more_objects.end(),Kill());
of.WriteToBitmap("render_test.bmp");
return 0;
}
|
Given task
Given a container of Object pointers, you are to render the geometric objects that they correspond to by invoking the (polymorphic) Render member function of each object. Recall however that each object has a depth parameter that determines the order in which the objects should be rendered. You are to write a function template named RenderObjects that renders the container of objects in depth order. Some things to consider here: we want the function to be used with as many sequential containers as possible; however, we will need to sort the objects in the container according to the object depth.
There are two problems. First, sorting a list and sorting a vector (or deque) require two different methods: a list has a member function for this, while a vector/deque does not - it must use the STL sort algorithm (which requires that the class it is used with have a random access iterator). Second, the Object class does not have a built-in comparison operator, so we must provide one. There are thus two parts to this task. The first part is to create a binary function object, named ObjectCompare, which will take two Object pointers and return the result of comparing their depth values. Thus, if obj ptr1 and obj ptr2 are pointers to objects with depths and respectively, the code fragment
ObjectCompare obj_cmp(); bool r = obj_cmp(obj_ptr1, objt_ptr2);
will result in r being equal to true, since 1.3 < 2.9.
The second part is to define a function template RenderObjects that sorts and renders all of the objects in a container of Object pointers. Since STL list containers are sorted in a special way (through a member function), you will have to make a template specialization for containers that are lists of Object pointers. Specifically, the code fragment
std::deque<Object*> objects; std::list<Object*> more_objects; // ... objects place in the containers ... RenderObjects(objects); RenderObjects(more_objects);
will render the objects in each of the containers objects and more objects.
For this task, you will put all code in a single file, named RenderObjects.hpp.
You may only include the <algorithm>, <functional>, <list>, and Object.hpp header files. Also, you may not use any explicit loops, and you may only define one function object (namely ObjectCompare). Of course, you must not use the using keyword. IMPORTANT: Making use of lambda expresions would be considered as defining an extra function object.
here my folder link contain all the code and the pdf instruction
https://drive.google.com/file/d/1opApVb_M-LjVv--neUpfXP7chasZ3gWr/view?usp=sharing