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

Username:   Password: 
 RegisterRegister   
 Help with Headers
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
blackhawk_prince




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




PostPosted: Fri Aug 21, 2009 11:48 am   Post subject: RE:Help with Headers

I think you have a missing semi-colon in Employee.h
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
saltpro15




PostPosted: Fri Aug 21, 2009 1:11 pm   Post subject: RE:Help with Headers

c++:

#include <iostream>
#include <string>
#include "Employee.h";
using namespace std;

main()
{
   ...
return 0;
}
rdrake




PostPosted: Fri Aug 21, 2009 2:54 pm   Post subject: Re: RE:Help with Headers

saltpro15 @ Fri Aug 21, 2009 1:11 pm wrote:
c++:

#include <iostream>
#include <string>
#include "Employee.h";
using namespace std;

main()
{
   ...
return 0;
}
No.

What Tony said.
DemonWasp




PostPosted: 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).
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: