Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Template argument and eof()

  1. Jul 4, 2004 #1
    Two questions,
    1) Can you tell me why
    ifstream f("f.txt"); //there are 2 numbers in file f
    int count=0
    Why does there exist to be x[2] ? How can I chaneg the code to get only two x[0],x[1];

    2) Completely different though,
    Are there any ways to "parse" the values i.e integers as the template arguments at runtime ? My VC++ 6.0 compiler complains that i cant pass the numbers directly like that at run time.

    Thanks in advance,
    -Fiona's twin__001
  2. jcsd
  3. Jul 4, 2004 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Because the stream doesn't recognize eof until after you've actually attempted to read past the end of the file. A better solution would be

    Code (Text):

    while(1) {
    f >> x[count];
    if (f.eof()) break;
    - Warren
  4. Jul 4, 2004 #3
    Thanks chroot, -->lol
  5. Jul 5, 2004 #4
    Anyone has any ideas about my second question ???
  6. Jul 5, 2004 #5
    Well, with the limited info you provided I'd say your problem is you're trying to compile a program which relies on runtime values but the program requires those values before it can compile. Big catch-22 huh? Do a google search for "new C++ dynamic memory" and you'll find the answer. Here, a couple of links from said same search to get you started if I'm reading your problem correctly.


    Basically, you need to create a pointer of the desired data type then use the new operator so your compiler knows you want to use runtime variables.

    Hope this helped, good luck.
  7. Jul 5, 2004 #6
    faust9, I would like to make some questions,
    1) What is Big-catch 22 ?
    2) Thank faust9 very much for those links, but my problem I think is not about memory allocation...My program asks the user to input a number and it will output numbers circling with radius 5, around the input, all are of course integers. But I tried using template in my program and I would like to put that input number in something like Circle<num> which is where compiler give me lots of complaints...Do you know how I can do something like std::cout<<Circle<Num> ?
  8. Jul 5, 2004 #7


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Template parameters must be constants; there is simply no way around this. While there are lots of clever ways to compute constants at compile-time, the fact remains that they are still constants; there is absolutely no way to grab a runtime value and turn it into a compile-time constant.
  9. Jul 6, 2004 #8
    Big catch 22 might mean using 22 to have a big catch /--lol--\
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Template argument and eof()
  1. Command line argument (Replies: 16)

  2. Argumented Matrix (Replies: 2)