C++ Program Bug need Help

Hey,

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?

here is the code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

//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>

using namespace 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>

using namespace 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;
}
Last edited on
You need to return your inner vector by reference, otherwise you are just modifying a copy of the vector.
true, so u mean like this ?

std::vector<obj> &renderer::getVector()
{
return g.getVector();
}

getting the error

invalid initialization of non-const reference of type 'std::vector<obj>&' from an rvalue of type 'std::vector<obj>'
return g.getVector();
^

tried to fix this but actually have no idea with the const problem
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.
thanks man.
it's working now :)
Topic archived. No new replies allowed.