Computer Science Canada

Top 5 Most Common Silly C++ Mistakes

Author:  Asok [ Mon Mar 10, 2003 10:47 pm ]
Post subject:  Top 5 Most Common Silly C++ Mistakes

I decided to make this list after being frustrated over an error that makes no sence because of a little thing, if you come up in one of these situations, it might be a good idea to check this list.

5. You're missing an #include
Keep in mind that your .cpp's should have includes to thier respective .h's. Also be sure to include addition header files if you have an inherit system (ie. Cube inherits from the Object class so in Cube.h you need to include Object.h or else you will recieve an undeclared identifier error) Another thing I see often with includes is when to use quotes "" and when to use brackets <>, use quotes when the file is in the same directory, use brackets when it is an external file such as, stdio.h so it should look like #include <stdio.h>.

4. You've misspelled something
Dan probably runs into this a lot Wink , but we all do. It is probably one of the hardest errors to find (provided it's not just a misspelled variable) C++ does not color in as much text as turing does so you need to be a bit more cautious. It's even worse when you actually have spelt everything correctly however C++ is case sensitive and you've skipped over something. (Such as, glBindTexture, if you don't spell it with a capital B and T it will mess up on you!) The best way to avoid this with your own variables and classes is consistency. Find a way you are most confortable with (ie. Captializing the first letter of classes, and lower case the first word in variables.) of course, if you are working with external files and variables you then have to double check how it is spelled in the external file.

3. You're missing a {bracket}
Most people are confident that they will not have to worry about brackets however this appears the most out of neglagence. There are 2 simple methods to keep track of you brackets: 1. The second you put and open bracket, put a close bracket right next to it then work inside. 2. Indent everything, it's annoying to pay attention to but it pays off if you are looking for a missing bracket and it keeps your code looking neat.

2. You're missing a semicolon;
This error pops up a lot and everyone has run into it. It's typically easy to find as the debugger will typically say missing ';' at line whatever. However, if you're missing it in a header file, you'll run into a slew of errors because the compiler still thinks you were declaring a previous variable because the semicolon acts as a breaker. When you get the most assinine errors, this is typically the reason.

1. You've used a colon: instead of a semicolon;
This is definitely one of the easiest to do and one of the hardest to locate errors that can typically arise. This is because it's easy to start (colon and semicolon are on the same bloody key) and it's hard to find when scanning code. The colon and the semicolon look very much alike, especially when surrounded with code. I can't really give much advice on finding this one other than, look very carefully, and if you're tired, give up and do it in the morning. You will not be doing yourself any favours by trying to compare the difference of a few pixels while tired, especially when your mind gets lazy and your eyes wander through the code.

That's it for my Top 5 Most Common Silly C++ Mistakes, I hope this helps you out so you can find the silly little things rather than getting frustrated and beating your computer. Over 90% of errors you encounter will probably be on this list (this statistic was made up by me and means nothing like all other statistics.) Very Happy

Author:  Tony [ Mon Mar 10, 2003 11:46 pm ]
Post subject: 

donno about you guys, but the single mistake I run into most... is forgeting to type up using namespace std; right after all the includes.

It might not be nesesary to have it for some of the programs, but if you're using namespace standarts for coding, you'll get a bunch of weird errors you wouldn't be able to figure out.

Also.. be aware of semicolon;s. Not that you're missing it (debugger will tell you) but adding an extra one. It will compile and not show any errors. BUT your program will not run the right way.

I'm serios... while preparing for CCC contest myself and Brad (kid in my compsci class) were going over some problems. I explained the solution and he typed it up, but it didnt work. It took us half an hour to figure out why his program didnt work and we had to print out both programs (mine and his) and go over them LINE BY LINE comparing. They turned out to be identical, but he mistakenly put a ; after an if statment Shocked

that basically throughs out your statment and runs the code reguardless of conditions.


Another problem I find a lot is using == in if statments for comparisment. Belive it or not, if you use a single = it will be treated as an assigment statment and your variable will get a brand new value Twisted Evil

Those bugs are a pain in way too many places to find because your program compiles and runs, but you get those logical errors and incorrect output.

Oh... also... while declearing arrays, you cant have more then 60,000 +- cells in it. Your program actually crashes and gives a pointer error or something.

Author:  Asok [ Tue Mar 11, 2003 12:19 am ]
Post subject: 

yea tony, I know there are more than 5 common errors, I just wanted a nice number. If I made it longer I would have definitely added in double operands such as ==, &&, ||. But the difference between == and = in C++ is just something you develop over time. It's the same thing with = and := in turing. I hope you liked the list. I figured I should probably put something in this forum, its had 0 threads for way too long. And since the site is geared towards those who are new in the language I figured this list would be a nice addition.

I might get around to doing additional tutorials... if anyone actually reads this forum Rolling Eyes

EDIT: btw, another common mistake I do is forgetting to put a semicolon at the end of classes in a header file. }; just doesn't look right and you don't do it very often, you'd figure the bracket could be its own breaker. Confused

Author:  Tony [ Tue Mar 11, 2003 12:24 am ]
Post subject: 

its nice of you... I'm catching up on putting up some nice tutorials for turing.

But as soon as those guys move onto higher languages as C++ and VB, we'll have lots of work to do in all areas.

If you want, post "hello world" program tutorial Wink Its actually a good start for programming in ANY language.

Author:  UBC_Wiskatos [ Tue Jul 01, 2003 3:40 pm ]
Post subject: 

Whoa, I hear you there, buddy!

I think the biggests mistakes I usually make are in an if statement, I'll go:

if(foo = true)

(which makes it true, always) instead of:

if(foo == true)

It's a bitch to spot too, because the compiler says nothing. Some programmers use:

if(true == foo)

However, I don't know, I find that sort of counter-intuitive. The second mistake I make is forgetting to put a semi-colon, but that's been occuring less and less frequently, although when I switch to Turing I put tons of semi-colons everywhere and then the thing creates an error.

Author:  Catalyst [ Tue Jul 01, 2003 3:44 pm ]
Post subject: 

ya thats an annying mistake

a similar one:

code:

for (int i=0;i=3;i++)


or sometimes ill just forget to initialize the for loop so the program will crash

Author:  krishon [ Tue Jul 01, 2003 4:41 pm ]
Post subject: 

lol...ye...and the nesting...i always screw up cuz of the brackets...like asok mentioned

Author:  SilverSprite [ Tue Jul 01, 2003 4:41 pm ]
Post subject: 

you dont need to put the intialize part in a for loop though? what do you mean?

Author:  krishon [ Tue Jul 01, 2003 4:43 pm ]
Post subject: 

u dun't need it, it just depends on wut u are doin

Author:  Catalyst [ Tue Jul 01, 2003 4:52 pm ]
Post subject: 

i mean i sometimes do this

code:

for (int i; i>10;i++)

Author:  Homer_simpson [ Tue Jul 01, 2003 4:53 pm ]
Post subject: 

1. You've used a colon: instead of a semicolon;
that one made me rewrite my whole source code!!!

Author:  gigaman [ Thu Dec 11, 2003 6:47 pm ]
Post subject: 

My most common mistake is i either put an extra semicolon or i don't put a semicolon. The worst is an extra. I once had to print out my whole code to find the mistake.

Author:  aliveiswell [ Tue Dec 16, 2003 6:11 pm ]
Post subject: 

i sometimes forget to change the direction of the greater than/less than signs for cin's.

Author:  AsianSensation [ Tue Dec 16, 2003 8:34 pm ]
Post subject: 

here is a common mistake, or maybe it's just me...

in VC++, when you don't close the run window and then try to compile your code, it gives you a fatal error, and since I don't lock my taskbar, I didn't check to see what was running, so I spent half an hour trying to debug my perfectly fine program.

needless to say, that was gay. I'm going to lock my taskbar as soon as possible and make it stay locked.

Author:  Mr. Gruntsworthy [ Fri Nov 24, 2006 11:15 am ]
Post subject: 

Erm... I've learned a little about C++ like variables, but where would a good explanation of the different loops be

Author:  Null [ Sat Nov 25, 2006 11:34 am ]
Post subject: 

I know it seems like a lot of extra typing, but I just prefix all standard library things with std:: instead of using namespace std. I always know what I haven't written myself (part of the standard library), and it makes code easier to read IMHO.

Author:  Mr. Gruntsworthy [ Sun Nov 26, 2006 1:14 pm ]
Post subject: 

for me, i always use Using Namespace std; i was just taught by the book to do it that way.

Author:  wtd [ Sun Nov 26, 2006 1:15 pm ]
Post subject: 

Understanding why you do that is more important than knowing how to.

Author:  Mr. Gruntsworthy [ Sun Nov 26, 2006 1:19 pm ]
Post subject: 

i understand why you use using namespace std;, its because it immediately makes that library available to the whole program if you announce it before or after your #includes, whereas the std:: prefix you have to use before every cout and cin

Author:  wtd [ Sun Nov 26, 2006 1:34 pm ]
Post subject: 

It doesn't affect whether or not you can use the code in the header. It simply includes the "std" namespace into your program's namespace, such that members of the std namespace declared in any header files that have been included can be used without explicitly denoting the namespace they were declared in.

Consider:

code:
namespace foo
{
   int bar()
   {
      return 42;
   }
}

int main()
{
   cout << foo::bar() << endl; // ok
   
   using foo::bar;             // also ok
   cout << bar() << endl;

   using namespace foo;        // also ok
   cout << bar() << endl;
}

Author:  Mr. Gruntsworthy [ Mon Nov 27, 2006 11:04 am ]
Post subject: 

well, i think for the sake of simplicity, im going to stick with the way i was taught, until i learn most of the language.

Author:  md [ Mon Nov 27, 2006 5:10 pm ]
Post subject: 

Mr. Gruntsworthy wrote:
well, i think for the sake of simplicity, im going to stick with the way i was taught, until i learn most of the language.


If your taught something that is wrong and you stick to it you will most likely suffer later on. It's much better to know something is right then simply know it works.

Author:  Mr. Gruntsworthy [ Mon Nov 27, 2006 11:02 pm ]
Post subject: 

but whats wrong with announcing using namepace for the whole program?

Author:  md [ Mon Nov 27, 2006 11:26 pm ]
Post subject: 

The same thing with including headers you don't use. The more things you include in your source, and the more things you include in your namespace; the more likely you are to run into nameing collisions. Using std::* instead of using namespace std; also increases readability because it's easy to pick out code that uses the standard libraries (and you really don't need to debug the stl... it's best not to try).

Yes, both ways work; but one is better then the other. Razz

Author:  Mr. Gruntsworthy [ Tue Nov 28, 2006 8:07 am ]
Post subject: 

Oh well... back on topic, one of my most common mistakes is spelling errors. Every time i write some code i have to go through it and find errors i made while typing.

Author:  Andy [ Tue Nov 28, 2006 6:46 pm ]
Post subject: 

okay, this one isnt exactly c++, but it killed me.

when debugging your code, make sure you check every single line, even the obvious ones. forgetting to clear the Z buffer in a d3d application will give you the strangest results.

Author:  Mr. Gruntsworthy [ Wed Nov 29, 2006 2:04 pm ]
Post subject: 

erm... I dont even know what that is... as ive stated, ive only just started learning C++ a little while ago...

Author:  Clayton [ Wed Nov 29, 2006 5:42 pm ]
Post subject: 

check your loop's conditions, the difference between a < or a > can be a significant one!

Author:  Mr. Gruntsworthy [ Wed Nov 29, 2006 6:59 pm ]
Post subject: 

okay, so would
Quote:
while (variable <= 100)

mean that the variable has to be 99 or under to exit the while loop? And what are the other loops[/quote]

Author:  Clayton [ Wed Nov 29, 2006 7:05 pm ]
Post subject: 

as long as your variable is under 100, that loop will continue to run, so if that condition is true, the loop continues, if it is false, it exits.

Author:  [Gandalf] [ Wed Nov 29, 2006 8:31 pm ]
Post subject: 

Mr. Gruntsworthy, you know this topic isn't only about you, but a general "Top 5 Most Common Silly C++ Mistakes" topic..?

As for that loop, it will exit when variable is not less than or equal to 100. In other words, when the variable becomes greater than 100.

Author:  Mr. Gruntsworthy [ Wed Nov 29, 2006 8:53 pm ]
Post subject: 

I know that, thank you very much. Im not blind, thank you.

Loop errors are a mistake made in C++, so since i was asking something, perhaps this does have to do with the topic.

Author:  [Gandalf] [ Thu Nov 30, 2006 12:49 am ]
Post subject: 

Really? Then, out of curiosity, explain to me why you would respond with the following:
Mr. Gruntsworthy wrote:
erm... I dont even know what that is... as ive stated, ive only just started learning C++ a little while ago...

To Andy's post about Direct 3D when it had nothing to do with you?

Author:  Mr. Gruntsworthy [ Thu Nov 30, 2006 9:27 am ]
Post subject: 

what is this, 'everybody piss of Mr. Gruntsworthy' day? I asked a question that needed answering. Get over it.

Author:  Mazer [ Thu Nov 30, 2006 10:06 am ]
Post subject: 

Mr. Gruntsworthy wrote:
what is this, 'everybody piss of Mr. Gruntsworthy' day? I asked a question that needed answering. Get over it.

Don't tell us to get over it, you just need to think a little. We have a whole freaking section for asking questions about C++, you don't need to bring up a 3 year old topic for it.


: