Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2010
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2010 by Ivor Horton; ISBN: 9780470500880
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 30th, 2012, 05:51 PM
Authorized User
 
Join Date: May 2012
Posts: 66
Thanks: 0
Thanked 4 Times in 4 Posts
Default basic debugging of programs

I remember how confused I was when I began using a compiler and received errors. For this reason I am writing this in an attempt to help those of you who have little or no programming experience. The example used here deals almost exclusively with syntax errors. The compiler errors generated may give a false indication of the problem. At a Later date, I may address problems which are coding errors not caused by syntax problems.

I strongly encourage enabling expert settings to give a more full featured menu and tool bar. It will give you more control as you progress through the book. To do this, select Tools and then Settings. Check the box for Expert Settings.

When building a program use the menu bar to select Build, then Build Solution instead of short cut keys. This may not make sense now. Later in this post I will explain why, and the reason will be more clear.
Code:
1   // sample.cpp
2   // This is a skeleton program based upon the example program named Ex2_02.
3   // Errors have been intentionally been included to demonstrate basic debugging 
4   // in a step-wise manner.
5
6   include <iostream>
7	
8   using std:cout;
9   using std::endl
10	
11  int main()
12  {
13  	  int num1 = 1234, num2 = 5678
14	  cout << endl;
15	  cout << num << num2;
16	  cout <  endl;
17
18	  return 0;
19  }
Here are the errors shown in the output pane.
Code:
------ Build started: Project: sample, Configuration: Debug Win32 ------
  sample.cpp
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(6): error C2143: syntax error : missing ';' before '<'
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(11): error C2039: 'endl' : is not a member of 'std'
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(11): error C2144: syntax error : 'int' should be preceded by ';'
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(11): error C2873: 'endl' : symbol cannot be used in a using-declaration
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(14): error C2146: syntax error : missing ';' before identifier 'cout'
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(14): error C2065: 'endl' : undeclared identifier
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(14): warning C4552: '<<' : operator has no effect; expected operator with side-effect
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(15): error C2065: 'num' : undeclared identifier
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(15): warning C4552: '<<' : operator has no effect; expected operator with side-effect
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(16): error C2065: 'endl' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The first rule of debugging should be to find the line number of the first error message. Examine all code on and before that line for errors.

The first error is on line 6 (sample.cpp(6)). That means that the error is somewhere in lines 1-6. The first compiler error is C2143: syntax error: missing ';' before'<'. That is not really accurate. The help entry for C2143 really does not give a helpful clue. This is a lesson in debugging. Before chasing error codes, look for syntax errors. Line 6 reads "include <iostream>" instead of the proper syntax "#include <iostream>". The compiler does not understand the statement without the "#".

Let's correct the syntax error, and Rebuild the solution. Select Build then Rebuild Solution as explained earlier. Now is the time for explaining using the manual technique instead of short cut keys. Sometimes your output will contain warnings in addition to errors. The warnings are basically messages from the compiler which tell you that the compiler can proceed with your code, but there may be unintended results. This gives you the opportunity to make changes before running your program.

If you simply Build the solution, existing warnings don't show in the output pane. (When you use the short cut keys, you Build, not Rebuild the solution.) Rebuilding the solution will cause any warnings to be displayed in the output pane. This gives you another opportunity to make changes before running the program.

Here is the corrected code from the first error correction.
Code:
1   // sample.cpp
2   // This is a skeleton program based upon the example program named Ex2_02.
3   // Errors have been intentionally been included to demonstrate basic debugging 
4   // in a step-wise manner.
5
6   #include <iostream>
7	
8   using std:cout;
9   using std::endl
10	
11   int main()
12   {
13	  int num1 = 1234, num2 = 5678
14	  cout << endl;
15	  cout << num << num2;
16 	  cout <  endl;
17
18	  return 0;
19   }
Here are the errors displayed in the output pane.
Code:
------ Rebuild All started: Project: sample, Configuration: Debug Win32 ------
  sample.cpp
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(8): error C2143: syntax error : missing ';' before ':'
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(8): error C2873: 'std' : symbol cannot be used in a using-declaration
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(8): error C2059: syntax error : ':'
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(11): error C2144: syntax error : 'int' should be preceded by ';'
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(14): error C2146: syntax error : missing ';' before identifier 'cout'
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(14): error C2563: mismatch in formal parameter list
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(14): error C2568: '<<' : unable to resolve function overload
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(977): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1003): or       'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1011): or       'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
          with
          [
              _Elem=wchar_t,
              _Traits=std::char_traits<wchar_t>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1021): or       'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
          with
          [
              _Elem=unsigned short,
              _Traits=std::char_traits<unsigned short>
          ]
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(15): error C2065: 'num' : undeclared identifier
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(15): warning C4552: '<<' : operator has no effect; expected operator with side-effect
c:\users\myname\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(16): error C2563: mismatch in formal parameter list
c:\users\name\documents\visual studio 2010\projects\beginning visual c++\sample\sample\sample.cpp(16): error C2568: '<' : unable to resolve function overload
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(977): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1003): or       'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1011): or       'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
          with
          [
              _Elem=wchar_t,
              _Traits=std::char_traits<wchar_t>
          ]
          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(1021): or       'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
          with
          [
              _Elem=unsigned short,
              _Traits=std::char_traits<unsigned short>
          ]
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
You will notice that there are now 14 compiler errors. That is two more than before making the first correction. Why? Now the compiler is attempting to perform actions which it could not before the preprocessor directive #include <iostream> was corrected. This is an example of why debugging should begin with the first line flagged as an error and inspecting that line and all previous lines for syntax problems.

Now the first flagged error is line 8. Once again this is a syntax error. The compiler error and description in the output pane really don't address this because the compiler doesn't know what you meant to type.

The error is the use of ":" instead of "::". Both ":" and "::" are legal operators. This why the compiler gave the wrong error. The correction here would be to make line 8 read std::cout;. After making the necessary change, choose Build then Rebuild Solution again.

You will get more errors. This post would turn into a book if I followed all the steps for debugging the program here. I strongly encourage you to complete the debugging for the experience of finding and correcting errors in the code to see what to expect in programs of your own before getting into more advanced programs. It will make more complex programs easier to debug because you will automatically correct simple syntax errors before attacking other errors.

I am going to give you all the errors here; so you don't miss anything in the exercise. I strongly recommend attempting to find them without consulting the following list. If you need help, consult the list.
  • line 9: missing semicolon -- should read using std::endl;
  • line 13: missing semicolon -- Line 13 should read int num1 = 1234, num2 = 5678;.
  • line 15: variable name not initialized-- Line 13 declares and initializes num1 not num. Correct line 15 to read cout << num1 << num2;.
  • line 16: syntax error again ('<' used instead of "<<") It should read cout << endl;.
.

I hope you find this useful.

drpepper

Last edited by drpepper; June 30th, 2012 at 05:54 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic problem debugging first app? crashnburn63 BOOK: Professional Android 2 Application Development 2 February 17th, 2011 03:45 PM
Image Programs bmains Intro Programming 1 March 9th, 2006 08:54 AM
executing programs marjanm PHP How-To 0 August 27th, 2004 06:23 AM
error on programs mxm331 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 June 20th, 2004 08:49 AM
EXE Programs zhenwe1 Servlets 1 December 1st, 2003 10:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.