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