473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help: Can't access more than one element in class

Hello,

I'm having problems accessing elements in the polygon class every time
I try to move the iterator it stays at the same element...
code:
#include <iostream>
using namespace std;

class Vertex
{
public:
Vertex(int x=0, int y=0, Vertex *n = NULL, Vertex *p=NULL)
{xcoord = x; ycoord=y; next=n; prev=p;}
int getx() {return xcoord;}
int gety() {return ycoord;}
void setx(int a) {xcoord = a;}
void sety(int b) {ycoord = b;}

private:
int xcoord, ycoord;
Vertex *next, *prev;
friend class Polygon;
};

class Polygon
{
public:
Polygon() {header = NULL; currItr=NULL;}
bool isEmpty(){return (header == NULL);}
void reset(){currItr = header;}
void forward(){if (currItr) {currItr = currItr->next;}}
void backward(){if (currItr) {currItr = currItr->prev;}}
void insertAfter(int x, int y);
void insertBefore(int x, int y){backward(); insertAfter(x,
y);}
void readPolygon();
void printPolygon();
double perimeter();
double area();
int findLeftmostxcoord();
int rindRightmostxcoord();
int findTopmostycoord();
int findBottommostycoord();
bool leftTurn();
bool isConvex();

private:
Vertex *header;
Vertex *currItr;

};

int main()
{
Polygon one, two;
one.readPolygon();
two.readPolygon();

cout<<endl;
one.printPolygon();


return 0;
}
void Polygon::readPolygon()
{
int x1, y1, n;
cin>>n;

for(int i=0; i<n; i++)
{
cin>>x1>>y1;
// cout<<x1<<" "<<y1<<" ";
insertAfter(x1, y1);
forward();
}
}
void Polygon::insertAfter(int x, int y)
{
Vertex *ptr = new Vertex(x, y, NULL, NULL);
//cout<<x<<" "<<y<<" ";
if (isEmpty())
{
ptr->prev = ptr;
ptr->next = ptr;
currItr = ptr;
header = ptr;
}

else
{
ptr->prev = currItr;
ptr->next = currItr->next;
currItr->next;
}
}

double Polygon::perimeter() //Find the perimeter of a polygon
{
Vertex *ptr1 = currItr;
double perm = 0.0;
double x1 = ptr1->getx();
double x2 = ptr1->next->getx();
double y1 = ptr1->gety();
double y2 = ptr1->next->gety();

double distance;
distance = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
perm = perm + distance;

return perm;
}

bool Polygon::leftTurn()
{
Vertex *ptr1 = currItr;

int x1 = ptr1->getx();
int x2 = ptr1->next->getx();
int x3 = ptr1->next->next->getx();

int y1 = ptr1->gety();
int y2 = ptr1->next->gety();
int y3 = ptr1->next->next->gety();

double a = x1*y2-y1*x2+y1*x3-x1*y3+x2*y3-x3*y2;

return(a>0);
}

void Polygon::printPolygon()
{
Vertex *itr = currItr;

cout<<itr->getx()<<" "<<itr->gety()<<endl;
cout<<itr->next->next->next->getx()<<"
"<<itr->next->next->next->gety()<<endl;

}
Feb 25 '06 #1
1 1664
qwerty wrote:
I'm having problems accessing elements in the polygon class every time
I try to move the iterator it stays at the same element...
code:
[..]


You're not trying "to move the iterator". "Moving" the iterator would
actually mean changing its value. Something like

iter++

or

iter = iter->next;

V
--
Please remove capital As from my address when replying by mail
Feb 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
2
by: . . | last post by:
Hi I need some help with java script . I have a ASPX page that is pulling a user message heading from the table on the page . The user message header has message body which suppose to dispay...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.