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

  • Home
  • SEARCH
  • 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 9239819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:04:38+00:00 2026-06-18T08:04:38+00:00

Originally, I have some code that looks like class Ball { public: double x

  • 0

Originally, I have some code that looks like

class Ball
{
public:
    double x , y ;
    ofstream out ;
} ;

int main()
{
    Ball array[N] ;

    array[0].out.open("./position_1.txt") ;
    array[1].out.open("./position_2.txt") ;

    ......
}

where N is a run-time determined constant.
But it suffers variable length arrays problem recently.

I try to follow the suggestion by this post Can't set variable length with variable by using STL container.

int main()
{
    vector<Ball> Balls ;
    Ball b ;

    b.out.open( "./position_1.txt" ) ;
    Balls.push_back( b ) ;
    ......
}

It fails at push_bak(), since stream can not be copied.

I can’t determine the number of balls before running and I have to store the file stream instead of path for efficiency (preventing opening and closing files).

Is there any way to achieve the goal?
Thanks

  • 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-18T08:04:39+00:00Added an answer on June 18, 2026 at 8:04 am

    C++ streams are not copyable, but they’re movable, so you can do this instead:

    Balls.push_back( std::move(b) );
    
    //DO NOT use b after push_back, as it has been moved!
    

    and the default move-semantic generated by compiler for your class will work fine with this.

    In C++11, you can write this:

    std::vector<Ball> balls(N); //N is known at runtime
    
    balls[i].out.open( "./position_1.txt" ); //i <= i < N
    

    Note that this will not work in C++03, as in C++03 vector’s constructor creates N copies of a default created object of type Ball. In C++11, however, it doesn’t make any copy!

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

Sidebar

Related Questions

I have some code (which works fine) that looks something like this: int integer
For example I might have some css stuff that looks like this: .divType1 {
Edit: original question below, but I revise it now that I have some code
I have a class CContainer that has some members CMemberX , CMemberY , which
While maintaining some code, I discovered that we have an infinite hang-up in a
I have some code that currently builds up an In statement in SQL. I
I have a class that creates and uses a System.Threading.Timer, something like this: using
In some old Java code, I found a class that contains a lot of
Let's say I have some original text: here is some text that has a
I originally have an array that reads $array1 = array(a,a,b,b) I run it through

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.