C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
qwerty
Guest





PostPosted: Sat Feb 25, 2006 11:06 pm    Post subject: Help: Can't access more than one element in class Reply with quote



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;

}
Back to top
Victor Bazarov
Guest





PostPosted: Sun Feb 26, 2006 12:06 am    Post subject: Re: Can't access more than one element in class Reply with quote



qwerty wrote:
Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.