Variable declaration with `for' loop

This is a discussion on Variable declaration with `for' loop within the C++ Programming forums, part of the General Programming Boards category; The compiler complains about the last statement in the following block: Code: for ( int i = 0; i <= ...

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    Variable declaration with `for' loop

    The compiler complains about the last statement in the following block:
    Code:
    for ( int i = 0; i <= 10; i++ ) {
    
              String placeholder;
    
              // Desired value for `placeholder' on the ith iteration
              // is "ObjectRefi", where `i' is an integer that has been converted 
              // to a string and appended to "ObjectRef".
      
              placeholder = strcat("ObjectRef" , toString(index));
    
              // The compiler doesn't like this next statement:   
    
              SomeObjectRef placeholder = new SomeObjectRef();
    
    }
    Why? (No arrays allowed!)
    Resolution?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You're re-declaring placeholder in that code.
    Code:
    strcat("ObjectRef" , toString(index));
    I hope you don't actually do that, because string literals are supposed to be constant.

    If you're going to make up code that demonstrates a point, try to ensure that it is correct.

    But I know what you mean.

    Probably the best way to do that is to use only one variable if possible. If that's not possible, you could use something linear -- for example, appending a name onto a list of other names.

    You could also "unroll" the loop, e.g.,
    Code:
    for(int i = 0; i < 3; i ++) std::cout << i << std::endl;
    ->
    Code:
    std::cout << 0 << std::endl;
    std::cout << 1 << std::endl;
    std::cout << 2 << std::endl;
    But these are just hacks to get around what should be implemented with arrays. I know no arrays are allowed, but that's just plain stupid.

    You could always use a container instead of an array. Like a vector or something . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> SomeObjectRef placeholder = new SomeObjectRef();
    What are you trying to do? You can't generate variable names dynamically in C++.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    What are you trying to do? You can't generate variable names dynamically in C++.
    Standard C++, at any rate. Though you can sort of emulate it with a std::map.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> Standard C++, at any rate.
    You can do that in non-standard C++?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Kinda sorta. I was thinking of dlopen() and DLLs on Windows and that sort of thing. Not so much dynamic name generation as dynamic function name resolution.

    And I'm sure that someone's created a weird "dynamic-name-C++" variation with support for that . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Error in global variable declaration
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 10:50 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. variable declaration in H file
    By aleccher in forum C Programming
    Replies: 5
    Last Post: 04-02-2003, 05:18 PM
  5. variable declaration style
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2002, 01:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21