RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 293 | Replies: 1
Reply
Join Date: Sep 2007
Posts: 9
Reputation: Icetigris is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Icetigris's Avatar
Icetigris Icetigris is offline Offline
Newbie Poster

Adding element to a vector crashes it?

  #1  
Apr 28th, 2008
I'm trying to write a data structure for a basic 3D engine, but for some reason it always crashes when I try to add an index to the polygon vector. Anyone know what may be going on? Thanks

#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <gl/glut.h>
#include <windows.h>

using namespace std;

class vertex
{
  public:
	float x, y, z;

	vertex(float xCoord = 0.0, float yCoord = 0.0, float zCoord = 0.0)
	{
		x = xCoord;
		y = yCoord;
		z = zCoord;
	}
	void printVertex()
	{
	    cout << x << " " << y << " " << z << "\n";
	}

};

class polygon
{
  public:
	  int a, b, c;

  polygon(int aVert = 0, int bVert = 0, int cVert = 0)
  {
	  a = aVert;
	  b = bVert;
	  c = cVert;
  }

  void printPolygon()
	{
	    cout << a << " " << b << " " << c << "\n";
	}

};

class model
{
  public:
	  vector<vertex> Vertex;
	  vector<polygon> Polygon;
	  vector<vertex>::iterator vIterator;
	  vector<polygon>::iterator pIterator;
 /* 
  void printModel()
  {
	  cout << "Verticies:\n";
	  for(vIterator = Vertex.begin(); vIterator != Vertex.end(); vIterator++)
	  {
		  cout << *vIterator << "\n";
	  }
	  cout << "Indicies of verticies in polygons:\n";
	  for(pIterator = Polygon.begin(); pIterator != Polygon.end(); pIterator++)
	  {
		  cout << *pIterator << "\n";
	  }
  }
*/
};

void main()
{
	float x = 0.0;
	float y = 0.0;
	float z = 0.0;
	char blar = 'y';
	while(blar != 'n')
	{
		cout << "enter an x: ";
		cin >> x;
		cout << "enter a y: ";
		cin >> y;
		cout << "enter a z: ";
		cin >> z;

		vertex v = vertex(x, y, z);
		vertex u = vertex(y, z, x);
		vertex w = vertex(z, x, y);

		polygon p = polygon(0,2,1);

		model m;
		m.Vertex.push_back(v);
		m.Vertex.push_back(u);
		m.Vertex.push_back(w);

		//polygon 0 has verts v0, v2, v1 in counterclockwise order
		m.Polygon.at(0) = p; //CRASHES HERE
		//m.Polygon[0].b = m.Vertex[2];
		//m.Polygon[0].c = m.Vertex[1];

		cout << "Your vertex: ";
		//m.printModel();
		cout << "type n to quit: ";
		cin >> blar;
	}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Posts: 104
Reputation: Laiq Ahmed is on a distinguished road 
Rep Power: 3
Solved Threads: 11
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Adding element to a vector crashes it?

  #2  
Apr 28th, 2008
I think you forgot to push the polygon element in the vertex. first you need to push element in the vector then use vector's at (index) function.
if you want to assign, use array operator instead which returns the reference. and let you add element
like below

 m.Polygon.push_back (p);
// or this way.
m.Polygon[0] = p;

Check the documentation of at(..) function which throws exception when trying to access out of bound, it doesn't insert the new element, it is just used to modify the existing one or return the const reference if used as readable member.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:02 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC