PDA

View Full Version : Template argument and eof()


cellec
Jul4-04, 12:48 PM
Two questions,
1) Can you tell me why
ifstream f("f.txt"); //there are 2 numbers in file f
int count=0
while(!f.eof()){
f>>x[count];
count++;
}
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

chroot
Jul4-04, 02:43 PM
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


while(1) {
f >> x[count];
if (f.eof()) break;
count++;
}


- Warren

cellec
Jul4-04, 02:49 PM
Thanks chroot, -->lol

cellec
Jul5-04, 01:15 AM
Anyone has any ideas about my second question ???

faust9
Jul5-04, 01:35 AM
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.

http://www.jive.nl/~kamphuis/eff_c/dmemory.html
http://cplus.about.com/library/weekly/aa072502b.htm
http://www.cplusplus.com/doc/tutorial/tut3-4.html

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.

cellec
Jul5-04, 01:47 AM
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> ?

Hurkyl
Jul5-04, 10:03 AM
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.

Vance
Jul6-04, 01:54 AM
Big catch 22 might mean using 22 to have a big catch /--lol--\