[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# y my vector.front() valur is missing when i call it again?

Posted on 2004-11-11
Medium Priority
199 Views
here is my code
(this part of code is part of my prog)

my question is

why (problem about wrong value outputing)

-----**  22 , 10
-----**  22 , 15
-----**  22 , 15

are not

-----**  15 , 10
-----**  22 , 10
-----**  22 , 15

above is the output of the called  'ptVec"

below is output when i compiled
***************************************
scenario 'c'
********* GL_BEGIN
VirtWidth : 30
VirtHeight : 20
(CAL_W,CAL_H) : 15 , 10
(CAL_W,CAL_H) : 22 , 10
(CAL_W,CAL_H) : 22 , 15
start-end bh : 15,10,22,15
BH at 15 , 10 , 22 , 15
POINT (b4 BH) -> 15 , 10
BH at 15 , 10 , 22 , 10
POINT (b4 BH) -> 22 , 10
BH at 22 , 10 , 22 , 15
POINT (b4 BH) -> 22 , 15
BH at 22 , 15 , 22 , 15
size of vector : 3
**************************
-----**  22 , 10
-----**  22 , 15
-----**  22 , 15
BH at 22 , 10 , 22 , 15
***************************************

cout << "VirtWidth : " << VirtWidth << "\n";
cout << "VirtHeight : " << VirtHeight << "\n";

float temp1[2];      // start pt. temp
float temp2[2];
float temp3[2];
float temp4[2]; // end point

int newW;            // calculated point
int newH;            // calculated point

point myObj;

for(int i = 0; i < myvec.size() ; i++)
{
obj& tmp = myvec.at(i);

if ( tmp.objtype == obj::COL)
{
// cout << " is color " << "\n";

color[0] = tmp.col[0];
color[1] = tmp.col[1];
color[2] = tmp.col[2];

}
else if(tmp.objtype == obj::POINT)
{

newW = calW(tmp.pt[0]);
newH = calH(tmp.pt[1]);

////////////////////////////////

myObj.pt[0] = floor(newW);
myObj.pt[1] = floor(newH);

ptVec.push_back(myObj);

////////////////////////////////

cout << " (CAL_W,CAL_H) : " << newW << " , " << newH << "\n";

SetPixel(newW,newH,color[0],color[1],color[2]);

}
else if(tmp.objtype == obj::BEGIN)
{

}
else if(tmp.objtype == obj::IDEN)
{

}
}

point& PTtmp = ptVec.front();

temp1[0] = PTtmp.pt[0];            // FIRST POINT
temp1[1] = PTtmp.pt[1];

//point& PTtmp3 = ptVec.end();  ????
point& PTtmp3 = ptVec.at(ptVec.size()-1);

temp4[0] = PTtmp3.pt[0];
temp4[1] = PTtmp3.pt[1];

cout << "start-end bh : " << temp1[0] << "," << temp1[1] << "," << temp4[0] << "," << temp4[1] << "\n";
bh(temp1[0],temp1[1],temp4[0],temp4[1]);

for(int pt_pos = 0; pt_pos < ptVec.size() ; pt_pos++)
{
point& PTtmp2 = ptVec.at(pt_pos);

cout << " POINT (b4 BH) -> " << PTtmp2.pt[0] << " , " << PTtmp2.pt[1] << "\n";

temp2[0] = PTtmp2.pt[0];
temp2[1] = PTtmp2.pt[1];

////////////////////////////////////////////////
// CHECK if the next position is the last one

if((pt_pos+1) == ptVec.size())
{
// already pass the last position
// cout << "LAST\n";

}
else // if the next pos is not the end of vector
{
PTtmp2 = ptVec.at(pt_pos+1);

temp3[0] = PTtmp2.pt[0];
temp3[1] = PTtmp2.pt[1];
}

bh(temp2[0],temp2[1],temp3[0],temp3[1]);
}

cout << "size of vector : " << ptVec.size() << "\n";

// ---------------------------------------------------
// edge walking
// ***************************************************

float t1[2];
float t2[2];
float t3[2];
point& grab = ptVec.front();

t1[0] = grab.pt[0];
t1[1] = grab.pt[1];

grab = ptVec.at(1);

t2[0] = grab.pt[0];
t2[1] = grab.pt[1];

grab = ptVec.at(2);

t3[0] = grab.pt[0];
t3[1] = grab.pt[1];
/*

check(temp1[0],temp1[1],grabP1.pt[0],grabP1.pt[1],temp4[0],temp4[1]);

cout << "-----**  " << temp1[0] << " , " << temp1[1] << "\n"; // 15,10
cout << "-----**  " << grabP1.pt[0] << " , " << grabP1.pt[1] << "\n"; // 22,10
cout << "-----**  " << temp4[0] << " , " << temp4[1] << "\n"; // 22,15

cout << " ************************** \n";
cout << "-----**  " << temp1[0] << " , " << temp1[1] << "\n";  // 15,10
cout << "-----**  " << temp2[0] << " , " << temp2[1] << "\n";  // 22,15
cout << "-----**  " << temp3[0] << " , " << temp3[1] << "\n";  //   "
cout << "-----**  " << temp4[0] << " , " << temp4[1] << "\n";  //   "
*/
cout << " ************************** \n";

cout << "-----**  " << t1[0] << " , " << t1[1] << "\n";
cout << "-----**  " << t2[0] << " , " << t2[1] << "\n";
cout << "-----**  " << t3[0] << " , " << t3[1] << "\n";
0
Question by:tom_mk
• 3

LVL 30

Expert Comment

ID: 12561851
It's hard to give you an answers, since you haven't shown us all your code.

What type is point, and can you post the class declaration for point?

What type is myvec?
0

LVL 30

Expert Comment

ID: 12561867

The following section of the code doesn't look right:

point& grab = ptVec.front();

t1[0] = grab.pt[0];
t1[1] = grab.pt[1];

grab = ptVec.at(1);

t2[0] = grab.pt[0];
t2[1] = grab.pt[1];

grab = ptVec.at(2);

t3[0] = grab.pt[0];
t3[1] = grab.pt[1];

First it gets a reference to the first object in ptVec.
Then it changes that object by setting it to the value at  ptVec.at(1)
0

Author Comment

ID: 12561874
all the code is quit long..
and need several file in order to compile..

this might help

struct obj
{

enum type  { POINT, COL, BEGIN, IDEN};

type objtype;

union
{
float pt[3];
float col[3];
};
};

// for GL_LINE_LOOP
struct point
{
float pt[2];

};

std::vector<obj> myvec;
std::vector<point> ptVec; // to keet the history of pt
0

LVL 30

Accepted Solution

Axter earned 2000 total points
ID: 12562501
Try changing your code to the following:
t1[0] = ptVec[0].pt[0];
t1[1] = ptVec[0].pt[1];

t2[0] = ptVec[1].pt[0];
t2[1] = ptVec[1].pt[1];

t3[0] = ptVec[2].pt[0];
t3[1] = ptVec[2].pt[1];
0

LVL 10

Expert Comment

ID: 12591006
Just a note,

when u use vectors, try to use iterators OR the size() function to have a bound check

Have a look at the following code for an example

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char** argv)
{

vector <int> p ;

p.push_back ( 1 ) ;
p.push_back ( 2 ) ;
cout << endl << "Using Indexes" ;
cout << endl << p [0] ;
cout << endl << p [1] ;
cout << endl << p [2] ;

cout << endl << "Using Iterators" ;
for ( vector<int>::iterator iter = p.begin();iter != p.end() ; iter ++ )
cout << endl << *iter ;

system ( "PAUSE" ) ;
return 0;
}

HTH

Amit
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month20 days, 13 hours left to enroll