Dynamic Allocation of 2D array of <class>

L

lestat

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
 
A

Alf P. Steinbach

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.
 
L

lestat

Alf said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top