Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8115933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:38:17+00:00 2026-06-06T03:38:17+00:00

could someone please tell me what I need to do in order to create

  • 0

could someone please tell me what I need to do in order to create an array of objects in a function (other than in the main function).

I will try to explain by making up some sort of example…

Let’s say I have a program named TimeScheduler.cpp that implements the class Schedule.h
(and I have the implementation in a separate file Schedule.cpp where we define the methods).

In the declaration file we have declared two constructors

  Schedule(); //the default

and

  Schedule(int, int, int);//accepts three arguments

to get to the point–let’s say in the main program file TimeScheduler.cpp we created our own functions in this program apart from the functions inherited from the class Schedule. so we have our prototypes listed at the top.

 /*prototypes*/

  void makeSomeTime();

etc…..

we have

 main(){ 


//etc etc...
 }

we then define these program functions

    void makeSomeTime(){
      //process
    }

let’s say that inside the function makeSomeTime(), we would like to create an array of Schedule objects like this

    Schedule ob[]={ 
       summer(5,14, 49), 
       fall(9,25,50)
    };

what do I have to do to the function makeSomeTime() in order for it to allow me to create this array of objects.
The reason I ask is currently i’m having difficulty with my own program in that it WILL allow me to create this array of objects in main()….but NOT in a function like I just gave an example of. The strange thing is it will allow me to create a dynamic array of objects in the function….. like

   Schedule *ob = new Schedule[n+1];
   ob[2]= Schedule(x,y,z);

Why would it let me assign to a non-dynamic array in main(), but not let me do that in the function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:38:19+00:00Added an answer on June 6, 2026 at 3:38 am

    This is not correct:

     Schedule ob[]={ 
           summer(5,14, 49), 
           fall(9,25,50)
        };
    

    You appear to be trying to introduce 3 new names:

    1. ob, which is an array of Scedules
    2. summer, which is a Schedule
    3. fall, which is a Schedule

    You can’t introduce summer and fall as new names like that. Perhaps this was just a typo, and you meant:

    Schedule ob[]={ 
       Schedule(5,14, 49), 
       Schedule(9,25,50)
    };
    

    …which is perfectly fine, and can exist in a function such as:

    void make_schedule()
    {
        Schedule ob[]={ 
           Schedule(5,14, 49), 
           Schedule(9,25,50)
        };
    }
    

    But now you have another problem — your make_schedule function returns void. The Schedule array you created in make_schedule is created and then just thrown away. If you want to return an array from a functtion, the best thing to do is to use a vector, and return that:

    std::vector<Schedule> make_schedule()
    {
        Schedule ob[]={ 
           Schedule(5,14, 49), 
           Schedule(9,25,50)
        };
    
        const size_t num_obs = sizeof(ob)/sizeof(ob[0]);
        std::vector<Schedule> ret;
        std::copy( &ob[0], &ob[num_obs], std::back_inserter(ret));
    
        return ret;
    }
    

    A poorer alternative is to use dynamic allocation to allocate your array, and return a pointer to the first element. In this case, when using new [] it’s important to note that you can only use the default constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please tell me which objects types can be tested using Regular Expressions
Could someone please tell me/link me to how I could create a method similar
Could someone please tell me whether the training sample sizes for each class need
Could someone please tell us on how to print correctly the handling thread in
Could someone please tell me how to use negation in the value of a
could someone please tell me why the dynamic_cast in the following code (five lines
I have the following: http://jsfiddle.net/mVs9T/13/ Could someone please tell me why the output is
Could someone please give me a example of a function that would use this
Could someone please tell me how can I change the default css of a
could someone please tell me the identifier for the @ key? for example keys.Escape

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.