Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 07-20-2002, 07:19 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
creating an array of an object on the freestore in c++ ??

i have a class named Album. I would like to create an array the size of 5 on the freestore.

PHP Code:
Albumdata[5] = new Album
i get the following error in my vc++6 compiler
Quote:
cannot convert from 'class Album *' to 'class Album *[5]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
Error executing cl.exe.
can anyone help me?

Last edited by sde; 07-20-2002 at 07:30 PM.
sde is offline   Reply With Quote
Old 07-20-2002, 07:35 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
fixed

well it seems like it didn't like me creating new objects on the same line that i was defining the array.. this seems to work:

Quote:
int main()
{
int const CAPACITY=5;
Album* data[CAPACITY];


for(int count=0;count<CAPACITY;count++)
{
data[count]= new Album;
data[count]= 0;
}



return 0;
}
sde is offline   Reply With Quote
Old 07-21-2002, 12:08 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
And yet, still theres something wrong with it..
you are creating an Album** with the Album* album[5];, if you wan't a static one, where you know it's gonna be 5, then use Album album[5];
Or if you wan't it to be a dynamicaly growing array, then use something like this:
Code:
int maxAlbums = 10;
int numAlbums = 0;
Album* albums;

albums = (Album*) malloc(maxAlbums * sizeof(Album));
if(!albums)
  {
    fprintf(stderr, "Not enough heap space\n");
    exit(-1);
  }
while (some_condition)
{
  if(numAlbums >= maxAlbums)
    {
       maxAlbums += 10;
       albums = (Album*)realloc(albums, maxAlbums*sizeof(Album));
        if(!albums)
          {
           fprintf(stderr,"Not enough heap space\n");
           exit(-1);
          }
      }
  albums[numAlbums++] = new Album;
}
Altho this is C, and not C++. I would guess using some kind of vector to store it in, would be the right thing to do. And then just use push and pop to extend it.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-21-2002, 02:18 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
thanks redhead, .. i haven't done much with it since i got the last thing to work , but i'm going to see what i can do now.

i appreciate it
sde is offline   Reply With Quote
Old 08-04-2002, 11:44 AM   #5 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
Kinda late in the game here, but I'll reply anyway...
Quote:
int main()
{
int const CAPACITY=5;
Album* data[CAPACITY];


for(int count=0;count<CAPACITY;count++)
{
data[count]= new Album;
data[count]= 0;
}



return 0;
}
There is a memory leak here. When you say data[count] = 0, the reference to the recently allocated Album is lost forever. However, what Redhead says is absolutely right. If you are using C++ you never have to deal with Arrays in this sense again, consider the following:
Quote:
#include <vector>
class Album { ; } // <-- implement this
int main() {
int const CAPACITY=5;
std::vector<Album*> albums;

for (int count=0; count<CAPACITY;count++) {
albums.push_back(new Album);
}
}
Now you have 5 freshly allocated Album objects, ready for use. To access albums, treat it just like an array:
Quote:
album[4]->displaySongList();
(or whatever methods are available).
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 08-04-2002, 12:30 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
thanks scott!

i thought data[count] = 0 was just assigning a null value to my pointers before i did anything with them.. i have a lot to learn.

i just finished my first c++ class last week, .. it was pretty basic, but i am still a beginner at it too.

what is this vector library? is there a good site i can go to find the most common used libraries, and what functions are built into them?
sde is offline   Reply With Quote
Old 08-04-2002, 12:57 PM   #7 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
std::vector<> is part of the Standard Template Library (STL), which was included in the standardization of C++ as part of the Standard C++ Library. It's free and it's extremely powerful!

There are quite a few container template classes available in STL, and once you get used to using them you'll wonder why you'd ever mess with any other language again. Seriously.

Here are a few good links to some of the best C++ writings out there:

STL
- SGI's STL programmers guide: http://www.sgi.com/tech/stl/
- STLPort is a free port of STL, used commonly for maximum compatibility: http://www.stlport.org/

C++ in general
- C++ Boost, some of the most talented C++ coders in the world: http://www.boost.org/ many of their ideas are being nominated for the next ANSI revision.
- C/C++ User Journal, pay close attention to the C++ Experts Forum: http://www.cuj.com/experts/?topic=experts More specifically, read anything and everything written by Scott Meyers or Herb Sutter.

Usenet
- comp.lang.c++.moderated, read anything and everything written by Andrei Alexandrescu. This guy is reinventing the way a lot of people thing about programming, and meta-programming specifically.

If I think of anything else I'll drop a note.
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 08-04-2002, 01:39 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
Thanks a lot Scott!

This information should be permanent on this site somewhere. I think i'll include it in the version i'm working on if ya don't mind for c++ resources.
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 08:37 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting