Dynamic Allocation of 2D array of <class>

Discussion in 'C++' started by lestat, Oct 11, 2003.

  1. lestat

    lestat Guest

    I have switched to a new compiler (gcc/g++ 3.2 on rh linux 8.0) and I
    cannot seem to dynamically allocate two dimentonal arrays (or even
    singel dimetional arrays!) of any user defined classes.

    Here is a simple example of an array of user defined class that *used*
    to work, but doesn't anymore:
    -------------------begin code-----------------------
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>

    using namespace std;

    class Pixel
    {
    public:
    Pixel();

    int i;
    int j;
    };

    int main()
    {
    Pixel p[4];

    for (int i=0;i<4;i++)
    {
    p.i=i;
    p.j=i*i;
    }

    for (int i=0;i<4;i++)
    {
    cout<<p.i<< " "<<p.j<<endl;
    }
    return 0;
    }
    ----------------------end code------------------

    Here is the error message I get when I compile:

    [root@voyager hw5]# g++ -o dblarray dblarray.cpp
    /tmp/ccMX0JlL.o: In function `main':
    /tmp/ccMX0JlL.o(.text+0x30): undefined reference to
    `Pixel::pixel[in-charge]()'
    collect2: ld returned 1 exit status
    [root@voyager hw5]#

    I get the same error if I try to allocate pointers of pointers etc...

    Any idea what I'm missing?

    --Dave
     
    lestat, Oct 11, 2003
    #1
    1. Advertising

  2. On Fri, 10 Oct 2003 23:04:11 GMT, lestat <> wrote:

    >I have switched to a new compiler (gcc/g++ 3.2 on rh linux 8.0) and I
    >cannot seem to dynamically allocate two dimentonal arrays (or even
    >singel dimetional arrays!) of any user defined classes.
    >
    >Here is a simple example of an array of user defined class that *used*
    >to work, but doesn't anymore:
    >-------------------begin code-----------------------
    >#include <iostream>
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >using namespace std;
    >
    >class Pixel
    >{
    >public:
    > Pixel();
    >
    > int i;
    > int j;
    >};
    >
    >int main()
    >{
    > Pixel p[4];
    >
    > for (int i=0;i<4;i++)
    > {
    > p.i=i;
    > p.j=i*i;
    > }
    >
    > for (int i=0;i<4;i++)
    > {
    > cout<<p.i<< " "<<p.j<<endl;
    > }
    > return 0;
    >}
    >----------------------end code------------------
    >
    >Here is the error message I get when I compile:
    >
    >[root@voyager hw5]# g++ -o dblarray dblarray.cpp
    >/tmp/ccMX0JlL.o: In function `main':
    >/tmp/ccMX0JlL.o(.text+0x30): undefined reference to
    >`Pixel::pixel[in-charge]()'
    >collect2: ld returned 1 exit status
    >[root@voyager hw5]#
    >
    >I get the same error if I try to allocate pointers of pointers etc...
    >
    >Any idea what I'm missing?


    You've declared a default constructor but never defined it.
     
    Alf P. Steinbach, Oct 11, 2003
    #2
    1. Advertising

  3. lestat

    lestat Guest

    Alf P. Steinbach wrote:
    > On Fri, 10 Oct 2003 23:04:11 GMT, lestat <> wrote:
    >
    >
    >>I have switched to a new compiler (gcc/g++ 3.2 on rh linux 8.0) and I
    >>cannot seem to dynamically allocate two dimentonal arrays (or even
    >>[snip]
    >>Any idea what I'm missing?

    >
    >
    > You've declared a default constructor but never defined it.
    >


    Well that was easy. Thank you very much, I've been stuck on this for hours!
     
    lestat, Oct 11, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rave

    dynamic array allocation

    Rave, Sep 25, 2003, in forum: C++
    Replies:
    5
    Views:
    628
    Kevin Goodsell
    Sep 25, 2003
  2. CodeMedic
    Replies:
    4
    Views:
    16,266
  3. Ken
    Replies:
    24
    Views:
    3,879
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    997
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,199
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page