Vector push_back changes values of previous elements.
Using vector in member function getPoints() shows weird behavior.
After pushing new objects to a vector, previous objects are assigned the new value.
And when the vector is returned I get garbage values.
The vector is not storing or using reference variables.
Can someone explain what's happening.
Thank you.
CODE
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
class Unit{
public:
double n[2];
double &x = n[0], &y = n[1];
Unit(double x=0, double y=0){
n[0] = x;
n[1] = y;
}
Unit& operator=(const Unit& rhs){
this->x = rhs.x;
this->y = rhs.y;
return *this;
}
Unit& operator+=(const Unit& rhs){
this->x += rhs.x;
this->y += rhs.y;
return *this;
}
Unit operator+(const Unit& rhs){
return Unit(x+rhs.x, y+rhs.y);
}
Unit& operator-=(const Unit& rhs){
this->x -= rhs.x;
this->y -= rhs.y;
return *this;
}
Unit operator-(const Unit& rhs){
return Unit(x-rhs.x, y-rhs.y);
}
Unit& operator*=(const Unit& rhs){
this->x *= rhs.x;
this->y *= rhs.y;
return *this;
}
Unit operator*(const Unit& rhs){
return Unit(x*rhs.x, y*rhs.y);
}
Unit operator*=(double rhs){
this->x *= rhs;
this->y *= rhs;
return *this;
}
Unit operator*(double rhs){
return Unit(x*rhs, y*rhs);
}
Unit operator/=(const Unit& rhs){
this->x /= rhs.x;
this->y /= rhs.y;
return *this;
}
Unit operator/(const Unit& rhs){
return Unit(x/rhs.x, y/rhs.y);
}
Unit operator/=(double rhs){
this->x /= rhs;
this->y /= rhs;
return *this;
}
Unit operator/(double rhs){
return Unit(x/rhs, y/rhs);
}
double dot(const Unit& rhs){
return x * rhs.x + y * rhs.y;
}
double len2(){
return dot(*this);
}
double len(){
return sqrt(len2());
}
double distance2(const Unit& rhs){
Unit C = *this - rhs;
return C.len2();
}
double distance(const Unit& rhs){
return sqrt(distance2(rhs));
}
Unit Normalize(){
return *this * 1.0 / len();
}
Unit Round(){
return Unit(round(x), round(y));
}
Unit Trunc(){
return Unit(int(x), int(y));
}
vector<Unit> getPoints(const Unit& rhs){
vector<Unit> points;
Unit start, end;
if(x < rhs.x){
start = *this;
end = rhs;
}
else{
start = rhs;
end = *this;
}
int run = end.x - start.x;
int rise = end.y - start.y;
double m = ((double) rise) / ((double) run);
double b = start.y - (m * start.y);
for(int i = start.x; i < end.x; ++i){
double y = (m * i) + b;
int rounded = (y > 0.0) ? floor(y + .50) : ceil(y - 0.5);
points.push_back(Unit(i, i));
cout<<"Expected Output : "<< points[points.size()-1].x << " " << points[points.size()-1].y <<endl;
}
cout << endl;
for (auto&p : points){
cout<<"Actual Output : "<< p.x << " " << p.y << endl;
}
return points;
}
};
int main()
{
Unit a, b(10,10);
auto c = a.getPoints(b);
cout <<endl;
for(auto& d: c){
cout<<"Recieved Output : "<< d.x << " " << d.y << endl;
}
return 0;
}
OUTPUT
Expected Output : 0 0
Expected Output : 1 1
Expected Output : 2 2
Expected Output : 3 3
Expected Output : 4 4
Expected Output : 5 5
Expected Output : 6 6
Expected Output : 7 7
Expected Output : 8 8
Expected Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Recieved Output : 2.07386e-317 6.9151e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
c++ stdvector
add a comment |
Using vector in member function getPoints() shows weird behavior.
After pushing new objects to a vector, previous objects are assigned the new value.
And when the vector is returned I get garbage values.
The vector is not storing or using reference variables.
Can someone explain what's happening.
Thank you.
CODE
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
class Unit{
public:
double n[2];
double &x = n[0], &y = n[1];
Unit(double x=0, double y=0){
n[0] = x;
n[1] = y;
}
Unit& operator=(const Unit& rhs){
this->x = rhs.x;
this->y = rhs.y;
return *this;
}
Unit& operator+=(const Unit& rhs){
this->x += rhs.x;
this->y += rhs.y;
return *this;
}
Unit operator+(const Unit& rhs){
return Unit(x+rhs.x, y+rhs.y);
}
Unit& operator-=(const Unit& rhs){
this->x -= rhs.x;
this->y -= rhs.y;
return *this;
}
Unit operator-(const Unit& rhs){
return Unit(x-rhs.x, y-rhs.y);
}
Unit& operator*=(const Unit& rhs){
this->x *= rhs.x;
this->y *= rhs.y;
return *this;
}
Unit operator*(const Unit& rhs){
return Unit(x*rhs.x, y*rhs.y);
}
Unit operator*=(double rhs){
this->x *= rhs;
this->y *= rhs;
return *this;
}
Unit operator*(double rhs){
return Unit(x*rhs, y*rhs);
}
Unit operator/=(const Unit& rhs){
this->x /= rhs.x;
this->y /= rhs.y;
return *this;
}
Unit operator/(const Unit& rhs){
return Unit(x/rhs.x, y/rhs.y);
}
Unit operator/=(double rhs){
this->x /= rhs;
this->y /= rhs;
return *this;
}
Unit operator/(double rhs){
return Unit(x/rhs, y/rhs);
}
double dot(const Unit& rhs){
return x * rhs.x + y * rhs.y;
}
double len2(){
return dot(*this);
}
double len(){
return sqrt(len2());
}
double distance2(const Unit& rhs){
Unit C = *this - rhs;
return C.len2();
}
double distance(const Unit& rhs){
return sqrt(distance2(rhs));
}
Unit Normalize(){
return *this * 1.0 / len();
}
Unit Round(){
return Unit(round(x), round(y));
}
Unit Trunc(){
return Unit(int(x), int(y));
}
vector<Unit> getPoints(const Unit& rhs){
vector<Unit> points;
Unit start, end;
if(x < rhs.x){
start = *this;
end = rhs;
}
else{
start = rhs;
end = *this;
}
int run = end.x - start.x;
int rise = end.y - start.y;
double m = ((double) rise) / ((double) run);
double b = start.y - (m * start.y);
for(int i = start.x; i < end.x; ++i){
double y = (m * i) + b;
int rounded = (y > 0.0) ? floor(y + .50) : ceil(y - 0.5);
points.push_back(Unit(i, i));
cout<<"Expected Output : "<< points[points.size()-1].x << " " << points[points.size()-1].y <<endl;
}
cout << endl;
for (auto&p : points){
cout<<"Actual Output : "<< p.x << " " << p.y << endl;
}
return points;
}
};
int main()
{
Unit a, b(10,10);
auto c = a.getPoints(b);
cout <<endl;
for(auto& d: c){
cout<<"Recieved Output : "<< d.x << " " << d.y << endl;
}
return 0;
}
OUTPUT
Expected Output : 0 0
Expected Output : 1 1
Expected Output : 2 2
Expected Output : 3 3
Expected Output : 4 4
Expected Output : 5 5
Expected Output : 6 6
Expected Output : 7 7
Expected Output : 8 8
Expected Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Recieved Output : 2.07386e-317 6.9151e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
c++ stdvector
add a comment |
Using vector in member function getPoints() shows weird behavior.
After pushing new objects to a vector, previous objects are assigned the new value.
And when the vector is returned I get garbage values.
The vector is not storing or using reference variables.
Can someone explain what's happening.
Thank you.
CODE
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
class Unit{
public:
double n[2];
double &x = n[0], &y = n[1];
Unit(double x=0, double y=0){
n[0] = x;
n[1] = y;
}
Unit& operator=(const Unit& rhs){
this->x = rhs.x;
this->y = rhs.y;
return *this;
}
Unit& operator+=(const Unit& rhs){
this->x += rhs.x;
this->y += rhs.y;
return *this;
}
Unit operator+(const Unit& rhs){
return Unit(x+rhs.x, y+rhs.y);
}
Unit& operator-=(const Unit& rhs){
this->x -= rhs.x;
this->y -= rhs.y;
return *this;
}
Unit operator-(const Unit& rhs){
return Unit(x-rhs.x, y-rhs.y);
}
Unit& operator*=(const Unit& rhs){
this->x *= rhs.x;
this->y *= rhs.y;
return *this;
}
Unit operator*(const Unit& rhs){
return Unit(x*rhs.x, y*rhs.y);
}
Unit operator*=(double rhs){
this->x *= rhs;
this->y *= rhs;
return *this;
}
Unit operator*(double rhs){
return Unit(x*rhs, y*rhs);
}
Unit operator/=(const Unit& rhs){
this->x /= rhs.x;
this->y /= rhs.y;
return *this;
}
Unit operator/(const Unit& rhs){
return Unit(x/rhs.x, y/rhs.y);
}
Unit operator/=(double rhs){
this->x /= rhs;
this->y /= rhs;
return *this;
}
Unit operator/(double rhs){
return Unit(x/rhs, y/rhs);
}
double dot(const Unit& rhs){
return x * rhs.x + y * rhs.y;
}
double len2(){
return dot(*this);
}
double len(){
return sqrt(len2());
}
double distance2(const Unit& rhs){
Unit C = *this - rhs;
return C.len2();
}
double distance(const Unit& rhs){
return sqrt(distance2(rhs));
}
Unit Normalize(){
return *this * 1.0 / len();
}
Unit Round(){
return Unit(round(x), round(y));
}
Unit Trunc(){
return Unit(int(x), int(y));
}
vector<Unit> getPoints(const Unit& rhs){
vector<Unit> points;
Unit start, end;
if(x < rhs.x){
start = *this;
end = rhs;
}
else{
start = rhs;
end = *this;
}
int run = end.x - start.x;
int rise = end.y - start.y;
double m = ((double) rise) / ((double) run);
double b = start.y - (m * start.y);
for(int i = start.x; i < end.x; ++i){
double y = (m * i) + b;
int rounded = (y > 0.0) ? floor(y + .50) : ceil(y - 0.5);
points.push_back(Unit(i, i));
cout<<"Expected Output : "<< points[points.size()-1].x << " " << points[points.size()-1].y <<endl;
}
cout << endl;
for (auto&p : points){
cout<<"Actual Output : "<< p.x << " " << p.y << endl;
}
return points;
}
};
int main()
{
Unit a, b(10,10);
auto c = a.getPoints(b);
cout <<endl;
for(auto& d: c){
cout<<"Recieved Output : "<< d.x << " " << d.y << endl;
}
return 0;
}
OUTPUT
Expected Output : 0 0
Expected Output : 1 1
Expected Output : 2 2
Expected Output : 3 3
Expected Output : 4 4
Expected Output : 5 5
Expected Output : 6 6
Expected Output : 7 7
Expected Output : 8 8
Expected Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Recieved Output : 2.07386e-317 6.9151e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
c++ stdvector
Using vector in member function getPoints() shows weird behavior.
After pushing new objects to a vector, previous objects are assigned the new value.
And when the vector is returned I get garbage values.
The vector is not storing or using reference variables.
Can someone explain what's happening.
Thank you.
CODE
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
class Unit{
public:
double n[2];
double &x = n[0], &y = n[1];
Unit(double x=0, double y=0){
n[0] = x;
n[1] = y;
}
Unit& operator=(const Unit& rhs){
this->x = rhs.x;
this->y = rhs.y;
return *this;
}
Unit& operator+=(const Unit& rhs){
this->x += rhs.x;
this->y += rhs.y;
return *this;
}
Unit operator+(const Unit& rhs){
return Unit(x+rhs.x, y+rhs.y);
}
Unit& operator-=(const Unit& rhs){
this->x -= rhs.x;
this->y -= rhs.y;
return *this;
}
Unit operator-(const Unit& rhs){
return Unit(x-rhs.x, y-rhs.y);
}
Unit& operator*=(const Unit& rhs){
this->x *= rhs.x;
this->y *= rhs.y;
return *this;
}
Unit operator*(const Unit& rhs){
return Unit(x*rhs.x, y*rhs.y);
}
Unit operator*=(double rhs){
this->x *= rhs;
this->y *= rhs;
return *this;
}
Unit operator*(double rhs){
return Unit(x*rhs, y*rhs);
}
Unit operator/=(const Unit& rhs){
this->x /= rhs.x;
this->y /= rhs.y;
return *this;
}
Unit operator/(const Unit& rhs){
return Unit(x/rhs.x, y/rhs.y);
}
Unit operator/=(double rhs){
this->x /= rhs;
this->y /= rhs;
return *this;
}
Unit operator/(double rhs){
return Unit(x/rhs, y/rhs);
}
double dot(const Unit& rhs){
return x * rhs.x + y * rhs.y;
}
double len2(){
return dot(*this);
}
double len(){
return sqrt(len2());
}
double distance2(const Unit& rhs){
Unit C = *this - rhs;
return C.len2();
}
double distance(const Unit& rhs){
return sqrt(distance2(rhs));
}
Unit Normalize(){
return *this * 1.0 / len();
}
Unit Round(){
return Unit(round(x), round(y));
}
Unit Trunc(){
return Unit(int(x), int(y));
}
vector<Unit> getPoints(const Unit& rhs){
vector<Unit> points;
Unit start, end;
if(x < rhs.x){
start = *this;
end = rhs;
}
else{
start = rhs;
end = *this;
}
int run = end.x - start.x;
int rise = end.y - start.y;
double m = ((double) rise) / ((double) run);
double b = start.y - (m * start.y);
for(int i = start.x; i < end.x; ++i){
double y = (m * i) + b;
int rounded = (y > 0.0) ? floor(y + .50) : ceil(y - 0.5);
points.push_back(Unit(i, i));
cout<<"Expected Output : "<< points[points.size()-1].x << " " << points[points.size()-1].y <<endl;
}
cout << endl;
for (auto&p : points){
cout<<"Actual Output : "<< p.x << " " << p.y << endl;
}
return points;
}
};
int main()
{
Unit a, b(10,10);
auto c = a.getPoints(b);
cout <<endl;
for(auto& d: c){
cout<<"Recieved Output : "<< d.x << " " << d.y << endl;
}
return 0;
}
OUTPUT
Expected Output : 0 0
Expected Output : 1 1
Expected Output : 2 2
Expected Output : 3 3
Expected Output : 4 4
Expected Output : 5 5
Expected Output : 6 6
Expected Output : 7 7
Expected Output : 8 8
Expected Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Actual Output : 9 9
Recieved Output : 2.07386e-317 6.9151e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
Recieved Output : 6.9151e-310 6.95256e-310
c++ stdvector
c++ stdvector
asked Nov 26 '18 at 3:37
Tejas PandeyTejas Pandey
226
226
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your class has reference variables in it.
When you make a copy of the class via the default copy-constructor, the new object's reference variables will refer to the same objects that the source's references referred to. Which in this case will be member variables of the source object.
You probably intended each Unit
's references to refer to the members of that same Unit
. To do this you will need to write your own copy-constructor that initializes the references appropriately. (And you should write a move-constructor too).
NB. A better approach would be to not use the reference variables at all. You could use a member function double& x() { return n[0]; }
instead.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474491%2fvector-push-back-changes-values-of-previous-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your class has reference variables in it.
When you make a copy of the class via the default copy-constructor, the new object's reference variables will refer to the same objects that the source's references referred to. Which in this case will be member variables of the source object.
You probably intended each Unit
's references to refer to the members of that same Unit
. To do this you will need to write your own copy-constructor that initializes the references appropriately. (And you should write a move-constructor too).
NB. A better approach would be to not use the reference variables at all. You could use a member function double& x() { return n[0]; }
instead.
add a comment |
Your class has reference variables in it.
When you make a copy of the class via the default copy-constructor, the new object's reference variables will refer to the same objects that the source's references referred to. Which in this case will be member variables of the source object.
You probably intended each Unit
's references to refer to the members of that same Unit
. To do this you will need to write your own copy-constructor that initializes the references appropriately. (And you should write a move-constructor too).
NB. A better approach would be to not use the reference variables at all. You could use a member function double& x() { return n[0]; }
instead.
add a comment |
Your class has reference variables in it.
When you make a copy of the class via the default copy-constructor, the new object's reference variables will refer to the same objects that the source's references referred to. Which in this case will be member variables of the source object.
You probably intended each Unit
's references to refer to the members of that same Unit
. To do this you will need to write your own copy-constructor that initializes the references appropriately. (And you should write a move-constructor too).
NB. A better approach would be to not use the reference variables at all. You could use a member function double& x() { return n[0]; }
instead.
Your class has reference variables in it.
When you make a copy of the class via the default copy-constructor, the new object's reference variables will refer to the same objects that the source's references referred to. Which in this case will be member variables of the source object.
You probably intended each Unit
's references to refer to the members of that same Unit
. To do this you will need to write your own copy-constructor that initializes the references appropriately. (And you should write a move-constructor too).
NB. A better approach would be to not use the reference variables at all. You could use a member function double& x() { return n[0]; }
instead.
answered Nov 26 '18 at 3:46
M.MM.M
106k11120243
106k11120243
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474491%2fvector-push-back-changes-values-of-previous-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown