0

My professor sent back a short email:

int x = 100;
var HT*;
HT = new int[x];

Firstly: Compiling the code (with other stuff around it to make it "work"): error: 'var' does not name a type.

Second: This is for hash tables. Apparently this is the way to create an array of variable size according to the ANSI standard (G++ has extensions that we can't use or he'll shoot us).

Any ideas?

Johnny
  • 1,963
  • 4
  • 21
  • 24
  • `int *HT;` would make much more sense - the above isn't a syntax I'm familiar with. – Will A Sep 12 '10 at 04:17
  • 3
    Tell your professor to use `std::vector`. – GManNickG Sep 12 '10 at 04:19
  • @Johnny: It's a dynamic array container from the standard library. You should really get [a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++ so you can program good C++. – GManNickG Sep 12 '10 at 04:32
  • 1
    One more piece of off-topic advice: Consider giving more self-explanatory names to your variables. If you, one day, have 10,000 lines of code full of names such as `x`, `i`, `HT`, `qm`, `st`, etc., you're more likely to suffer from it (trying to understand what's going on) than to benefit (from having had to type less). – stakx - no longer contributing Sep 12 '10 at 10:22
  • I guess with `var` he meant `int` or `double` or ... . And by the way, as C++98 is also an ISO standard, even in the US you can refer to C++98 as an ISO rather than an ANSI standard. – Cedric H. Sep 12 '10 at 10:45

3 Answers3

3

var HT* is not valid C++ syntax (looks C#-inspired to me;-) -- int *HT is how, in C++, you declare HT to be a pointer to integer (which can hold the result of an array-new -- you'll have to remember to use delete[] and not plain delete when you're done, though!).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
1

It should be auto which is the equivalent of var in C#. But I think, if your professor does not allow usage of std::vector, he or she will neither allow usage of std::array nor the c++0x code elements. To my knowledge there is no var keyword wheter present nor intended in standard c++. There is one in C++/CLI, but after what I have red, if you just mention this acronym your professor gets a heart attack...

Paul Michalik
  • 4,331
  • 16
  • 18
0

change the var to an int which should get rid of the first error.

VoodooChild
  • 9,776
  • 8
  • 66
  • 99
  • not sufficient: the star is also in the wrong place, _after_ the identifier being declared and defined, while it should be _before_ it (see my answer). – Alex Martelli Sep 12 '10 at 04:19