Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Dynamic Allocation of 2D array of <class>

Reply
Thread Tools

Dynamic Allocation of 2D array of <class>

 
 
lestat
Guest
Posts: n/a
 
      10-10-2003
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=i;
p[i].j=i*i;
}

for (int i=0;i<4;i++)
{
cout<<p[i].i<< " "<<p[i].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:ixel[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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-10-2003
On Fri, 10 Oct 2003 23:04:11 GMT, lestat <(E-Mail Removed)> 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=i;
> p[i].j=i*i;
> }
>
> for (int i=0;i<4;i++)
> {
> cout<<p[i].i<< " "<<p[i].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:ixel[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.

 
Reply With Quote
 
 
 
 
lestat
Guest
Posts: n/a
 
      10-11-2003
Alf P. Steinbach wrote:
> On Fri, 10 Oct 2003 23:04:11 GMT, lestat <(E-Mail Removed)> 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Dynamic allocation of a 2 dimensional array CodeMedic C++ 4 05-26-2004 12:52 AM
dynamic array allocation Rave C++ 5 09-25-2003 06:02 PM



Advertisments