Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Readin in an array of point[] 1

Status
Not open for further replies.

ChrisMacPherson

Programmer
Jul 3, 2000
157
GB
Hello all,

I am having trouble creating an array of Point(s). At one time I even got an error stating something like "Internal Compiler Error" !! Which shocked me a little. I dont have that code to hand anymore but need to get this working.

It's hard to find anything on the net due to the word point being used with reference to pointers, so please dont think I'm being lazy and havent looked.

I need to have a file which contains a line of integers seperated by spaces which are read in by my function and inserted in to an array of type Point.

Therefore the 1st integer would be pointArray[0].x and the 2nd pointArray[0].y

Then the 3rd integer would be pointArray[1].x and the 4th pointArray[1].y etc. etc.

I know how to open and read the file and loop to the end of the file, so really it is just how to initialise the point array.

This seems to me like it should be really simple, I cant think why I'm having such difficulty.

Thanks for any help...

Chris MacPherson


 
Also, just in case it is this, what #include should I be using to make use of the Point type, if any.

Chris MacPherson


 
As you might know the count of points only when you have reached the end of file, you will meet problems in collecting the points in a Point[] object(because those kind of arrays are not resizable).

I suggest you use an std::vector<Point> object instead. As you are reading points, just push back them in your vector<Point> object.

Example :
Code:
std::vector<Point> points;

//open file
//...

while (not file.eof ())
{
  //read next point
  Point p = next_point (file);//dummy function "next_point"
  points.push_back (p);
}

Be aware that class "Point" must be a value type, i.e. a class that has :
- an empty and copy constructors,
- operator=

Code:
class Point
{
public:
  Point ()
  {
     //FIXME: implement it
  }

  Point (const Point& other)
  {
     //FIXME: implement it
  }

  Point& operator= (const Point& other)
  {
     //FIXME: implement it
     return *this;
  }

};

If you can't modify class "Point" then use a std::vector<Point*> object, and allocate Point with new.

--
Globos
 

I am not sure if this is what I'm looking for as havent come across the vetor type before. First of all though:

Lets say I had a cetain number of points to read in, say 20
and I want to read them in to an array of Points. How would you do that?


In the code you have given me, can I access the vector using and array index kind of idea - i.e. points[0] etc..?

If you could point to any tutorials on this vetor thing that would be of help also.

Thanks Globos

Chris MacPherson


 
STL (Standard Template Library) is really something to consider when you develop software with C++. The template class vector is one of the basics of this library. Here's is the website where you can find useful doc :

Doc for vector class is here :

You can find tons of tutorials on STL, just type "STL tutorial" in any web search engine. I can't tell you specific tutorials.
STL is pretty hard to understand at beginning, but it is a powerful container library, that is worth learning.

Back to your question:
>>In the code you have given me, can I access the vector using and array index kind of idea - i.e. points[0] etc..?

Yes, it is possible. Say you have :
Code:
#include "Point.hpp"  //your Point class header
#include <vector>
#include <iostream>
using namespace std;

int main ()
{
  vector<Point> points;

  //read points from a file
  //...

  //iterate on points and display their coordinates
  for (int i = 0; i < points.size (); i++)
    cout << "Point " << i 
         << "  X=" << points[i].x ()
         << "  Y=" << points[i].y ()
         << endl;

  return 0;
}

-Lower bound of a vector object is always "0"
-Upper bound of a vector object is always "size() - 1" (size () gives the count of items)

--
Globos
 
Hello, been busy learning and using vectors, they have certainly helped me a lot. It turned out that one of my lectures at university the following day was talking about the STL and containers!!

Thank you for your help Globos. You saved me a lot of time.



Chris MacPherson


 
>Upper bound of a vector object is always "size() - 1"

Unless the vector is infact empty.

/Per

&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top