Posted: Fri Aug 21, 2009 11:35 am Post subject: Help with Headers
Hi I am learning C++ using a Deitel Deitel book. I am using Visual C++ Express Edition 2008 as my compiler. I keep the same error with I try to use headers while creating classes:
//imports
#include <iostream>
#include <string>
#include "Employee.h"
using namespace std; //error line
I get the error:
error C2143: syntax error : missing ';' before 'using'
But if i add a ";" before "using" the program runs fine. How can my code not get a syntax error if there is a statement terminator in the beginning of a statement??
Secondly, this error does not occur with the deitel sample code:
// Fig. 3.17: fig03_16.cpp
// Create and manipulate a GradeBook object; illustrate validation.
#include <iostream>
using std::cout;
using std::endl;
#include "GradeBook.h" // include definition of class GradeBook
What is wrong with my code??
btw i tried rearranging the code following the deitel example:
#include <iostream>
#include <string>
using namespace std;
#include "Employee.h"
int main (void){...}
error C2628: 'Employee' followed by 'int' is illegal (did you forget a ';'?)
error C3874: return type of 'main' should be 'int' instead of 'Employee'
Sponsor Sponsor
Tony
Posted: Fri Aug 21, 2009 11:48 am Post subject: RE:Help with Headers
I think you have a missing semi-colon in Employee.h
Posted: Fri Aug 21, 2009 3:19 pm Post subject: RE:Help with Headers
The reason the semicolon before "using" works (and the reason that what saltpro posted works) is because of how #include works. When you say #include, all the preprocessor does is copy the contents of the file you include in place of the #include line. That's it. Putting a ; on the end of the line is like putting it at the end of the included file.
The better answer is to just put the semicolon at the end of the included file, as you should have done in the first place. Don't forget that class declarations end in }; not just }.
The semicolon is taken as a statement-end, not necessarily line-end, so that you can have syntax like:
code:
for ( int i = 0; i < 50; ++i ) {
This is why you can have a semicolon at the start of your statement (blank lines are valid statements).