Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Top 5 Most Common Silly C++ Mistakes
Index -> Programming, C++ -> C++ Tutorials
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Asok




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Mar 10, 2003 11:46 pm   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Asok




PostPosted: Tue Mar 11, 2003 12:19 am   Post subject: (No 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
Tony




PostPosted: Tue Mar 11, 2003 12:24 am   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
UBC_Wiskatos




PostPosted: Tue Jul 01, 2003 3:40 pm   Post subject: (No 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.
Catalyst




PostPosted: Tue Jul 01, 2003 3:44 pm   Post subject: (No 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
krishon




PostPosted: Tue Jul 01, 2003 4:41 pm   Post subject: (No subject)

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




PostPosted: Tue Jul 01, 2003 4:41 pm   Post subject: (No subject)

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




PostPosted: Tue Jul 01, 2003 4:43 pm   Post subject: (No subject)

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




PostPosted: Tue Jul 01, 2003 4:52 pm   Post subject: (No subject)

i mean i sometimes do this

code:

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




PostPosted: Tue Jul 01, 2003 4:53 pm   Post subject: (No subject)

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




PostPosted: Thu Dec 11, 2003 6:47 pm   Post subject: (No 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.
aliveiswell




PostPosted: Tue Dec 16, 2003 6:11 pm   Post subject: (No subject)

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




PostPosted: Tue Dec 16, 2003 8:34 pm   Post subject: (No 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.
Mr. Gruntsworthy




PostPosted: Fri Nov 24, 2006 11:15 am   Post subject: (No subject)

Erm... I've learned a little about C++ like variables, but where would a good explanation of the different loops be
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 35 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: