I've got a problem in the following program:
(actually it's a little simulation of another program, so i've chosen really bad variable names/function names)
My main target is:
1) to create an object with width and length createObject(float,float)
2) add the Object to a vector (group)
3) to draw( cout ) all objects in the vector (cout width and length of the object)
4) straight edit width and legth of any object in the list(x,y)
task 1-3 is working fine.
but somehow when i try to set a new x and y in my code, it wont get changed in my vectors object (start values remain). anyone can help?
//main
#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <renderer.h>
#include "obj.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
renderer r;
r.creatObject(555,153);
r.draw();
cout << "------------MOVE SECTION------------ // NOT WORKING //" << endl;
r.getObj(0).setX(10); //set x value of the object by index
r.getObj(0).setY(55); //set y value of the object by index
r.getVector().at(0).setX(888); //get vector -> object by index -> set x value
r.getVector().at(0).setY(999); //get vector -> object by index -> set y value
r.draw();
//output is still 555 and 153
return a.exec();
}
//something like a controller class
#include "renderer.h"
#include <iostream>
usingnamespace std;
renderer::renderer()
{
}
//create an object and its x and y values
void renderer::creatObject(float x, float y)
{
obj o;
o.setX(x);
o.setY(y);
//add it to the group(vector)
//g -> Group g; in header
g.add(o);
}
//call the group function gdraw()
void renderer::draw()
{
//g -> Group g; in header
g.gdraw();
}
//return vector/group size
int renderer::size()
{
//g -> Group g; in header
return g.size();
}
// get object straight by index
obj renderer::getObj(int index)
{
return g.get(index);
}
//get whole vector
std::vector<obj> renderer::getVector()
{
return g.getVector();
}
//group of objects class -> drived from obj class
#include "group.h"
group::group()
{
}
void group::add(obj ob)
{
//o -> std::vector<obj> o; in header
o.push_back(ob);
}
obj group::get(int location)
{
//o -> std::vector<obj> o; in header
return o.at(location);
}
void group::gdraw()
{
for(int i = 0; i < o.size(); i++)
{
//o -> std::vector<obj> o; in header
o.at(i).draw();
}
}
int group::size()
{
//o -> std::vector<obj> o; in header
return o.size();
}
std::vector<obj> group::getVector()
{
//o -> std::vector<obj> o; in header
return o;
}
//object class
#include "obj.h"
#include <iostream>
usingnamespace std;
obj::obj()
{
}
//output the values
void obj::draw()
{
cout << "x is= " << x << endl;
cout << "y is= " << y << endl;
}
//set the x value of the object
void obj::setX(int x)
{
this->x = x;
}
//set the y value of the object
void obj::setY(int y)
{
this->y = y;
}
//return the x value of the object
int obj::getX()
{
return x;
}
// return the y value of the object
int obj::getY()
{
return y;
}
You'll need to make them all references. From what I can see of your code, you will need to change to the following things:
- obj& render::getObj
- std::vector<obj>& renderer::getVector
- obj& group::get
- std::vector<obj>& group::getVector
The rvalue error is because you were returning a reference to the temporary copy of the vector returned by the group::getVector function.