Thread: Adding objects to an array

  1. #1
    Unregistered
    Guest

    Adding objects to an array

    i have a class

    class Example{
    private:
    int number;
    char grade;
    char name[50];
    public:
    int getNumber();
    .
    .
    .
    };

    i have a method that returns a pointer to objects of this type.
    E.g.
    Example* returnObject();

    i want to add this into an array:

    Example* myArray=new Example[size];

    for(int i=0 ; i < 0 ; i++)
    newExample[i]=returnObject();

    //this doesn't work!

    can anyone please explain to me "how to add objects to a dynamic array, based on a function that returns a pointer to the object in question?


    cheers,
    Simon.

  2. #2
    Registered User cody's Avatar
    Join Date
    Sep 2001
    Posts
    86
    Hi,

    You declared your array as "myArray" so u gotta use that identifier when assigning your objects

    Not good
    Code:
    newExample[i]=returnObject();

    Try this:
    Code:
    myArray[i]=returnObject();

    aloa
    cody
    #include "reallife.h"

  3. #3
    Unregistered
    Guest
    i appologise for the code-error in my example,
    actually i've did it like you've stated it.

    Example* myArray=new Example[size];
    myArray[0]=returnFunction(); //this is in line 13

    //returnFunction is declared like this:

    Example* returnFunction();
    //and returns a pointer to an object of type Example

    i get this error (in Borland3):

    [C++Error] Example.cpp(13): Cannot convert 'Node *' to 'Node'.

    can you (anyone) help me?

    thanx

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Example* myArray=new Example[size];

    this declares an array of objects but you want an array of pointers so do this....

    Example** myArray=new Example*[size];
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  2. array of objects
    By sbook in forum C++ Programming
    Replies: 6
    Last Post: 06-01-2005, 02:13 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM