Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Klear

How Do I Create Multiple Objects Without Declaring Them, With A Loop?

This topic is 5317 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello All!, I am creating a loop that creates 6 clocks with random time zones. What I am unclear on is this: How do I create multiple objects without actually declaring them? I am trying to have a loop do it all for me. Do I do it by this?: Clock clock[y] ???? How do I add a letter or number to the end of the name of the object so it differs from the other? Thanks!
    for ( int y = 0; y < 7; y++){
        Clock clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
        xplace += 100;
        yplace += 100;
        clock.draw(page);
    }
[edited by - Klear on March 24, 2004 5:02:34 PM]

Share this post


Link to post
Share on other sites
Advertisement

Clock* clocks[7];

for ( int y = 0; y < 7; y++)
{
clocks[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock.draw(page);
}

Share this post


Link to post
Share on other sites
Thanks for the reply, but I am getting errors...


ClockDriver.java [52:1] not a statement
Clock* clocks[7];
^
1 error
Errors compiling ClockDriver.


[edited by - Klear on March 24, 2004 5:17:37 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Clock ** clocks

clocks = new (Clock *)[7];

for(i=0;i<7;++i)
clocks = new Clock();


for(i=0;i<7;++i)
delete clocks[i];

delete[] clocks;


Share this post


Link to post
Share on other sites
That s because you are using java and peter_b thought you used c++.


Clock clock[7];

for ( int y = 0; y < 7; y++){
clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock[y].draw(page);
}



LizardCPP

[edited by - LizardCPP on March 24, 2004 5:24:03 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.


Clock clock[7];

for ( int y = 0; y < 7; y++){
clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock[y].draw(page);
}



LizardCPP


Thanks man but...


ClockDriver.java [52:1] '']'' expected
Clock clock[7];
^
1 error
Errors compiling ClockDriver.

Share this post


Link to post
Share on other sites
quote:
Original post by Klear
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.


Clock clock[7];

for ( int y = 0; y < 7; y++){
clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock[y].draw(page);
}



LizardCPP


Thanks man but...


ClockDriver.java [52:1] ']' expected
Clock clock[7];
^
1 error
Errors compiling ClockDriver.



ah thats right....

you have to do Clock clock[] = new Clock[7]; first then the for-statement.

[edited by - LizardCPP on March 24, 2004 5:28:13 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by LizardCPP
quote:
Original post by Klear
quote:
Original post by LizardCPP
That s because you are using java and peter_b thought you used c++.


Clock clock[7];

for ( int y = 0; y < 7; y++){
clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
xplace += 100;
yplace += 100;
clock[y].draw(page);
}



LizardCPP


Thanks man but...


ClockDriver.java [52:1] ']' expected
Clock clock[7];
^
1 error
Errors compiling ClockDriver.



ah thats right....

you have to do Clock clock = new Clock[7]; first then the for-statement.



Thanks for hanging in Lizard Now its saying that I need an array? Are the brackets not doing that?

   
ClockDriver.java [52:1] incompatible types
found : Clock[]
required: Clock
Clock clock = new Clock[7];
^
ClockDriver.java [55:1] array required, but Clock found
clock[y] = new Clock(timeZoneIds[random], xplace, yplace);
^
ClockDriver.java [58:1] array required, but Clock found
clock[y].draw(page);
^
3 errors
Errors compiling ClockDriver.


[edited by - Klear on March 24, 2004 5:28:41 PM]

[edited by - Klear on March 24, 2004 5:29:22 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!