Monday, June 06, 2005

God bless C++ newbies

The C++ compiler, especially cl.exe that ships with visual c++ 6.0 can give you errors that are just plain wierd. For example, if you were writing a header file called "bangMyHeadOnWall.h":

class A {
/* class body */
}
<----- notice that you have forgotten to put a semi-colon. and then in your .cpp file, or in another .h file, you did this:

#include "bangMyHeadOnWall.h"


You will get the most strangest errors you've ever seen, actually errors on code that the compiler didnt complain about before.

Well even, the language syntax could give you a lot of pain, especially if you have learned your basics in Java - which is the way most CS students are taught these days. Here's some painful coding that you might have to do to dynamically allocate memory for a 2-dimensional array:

In Java: (o boy .. i miss java)
a = new int[3][4];

.. and here goes the c++ syntax:
int a**;
a = new int*[3];
for (int i=0;i<3;i++)
{
a[i] = new int[4];
}


However, C++ becomes more like Java when you want to allocate memory for a 2-dimensional array at compile time (You can get away with the subscript operators [][]) . But sadly, how often do powerful programs use statically allocated arrays!

0 Comments:

Post a Comment

<< Home