Thread: 1D array of class help

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    1D array of class help

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <dice.h>
    
    class Point{
    public:
    int x, y;
    };
    
    int main()
    {
    Point[] path = new Point[2];
    
    path[0].x = 1;
    path[0].y = 1;
    
    path[1].x = 3;
    path[1].y = 3;
           cout << path[0].x << " " << path[0].y << endl;
    
          system("PAUSE");
          return 0;
    }
    The above gives me a "parse error before [". How would I create a 1D class array for the Point class?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You are trying to allocate memory for a non-pointer type. You need to do one of the following:

    Code:
    Point *path = new Point[2];
    
    // blah
    delete [] path;
    Or...
    Code:
    Point path[2];
    
    //blah..

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    just think of it like a normal data type...

    Code:
    int array[5];
    and then replace with your data type

    Code:
    Point array[5];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM