need ; before using namespace std

This is a discussion on need ; before using namespace std within the C++ Programming forums, part of the General Programming Boards category; I can't seem to figure out why but I get a compile time error with some of my files if ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    need ; before using namespace std

    I can't seem to figure out why but I get a compile time error with some of my files if I don't put a ; before using namespace std eg)
    ;using namespace std;
    If I place the semicolon at the end of the line above or omit it altogether I will get an error. This is only with some files though and I can't seem to figoure out why. Below are two of my header files, one which needs the semi-colon and the other doesn't. If someone could point out why this is necessary it would be greatly appreciated.

    Here is one that requires it:
    Code:
    //--------------------------------------------------------------------------------------------------
    // Include Directive
    //--------------------------------------------------------------------------------------------------
    #ifndef AUTOMOBILE_H
    #define AUTOMOBILE_H
    
    #include <string>
    ;using namespace std;
    
    //--------------------------------------------------------------------------------------------------
    // A class that holds information about an automobilie including the Id, Make, Model, Mileage
    // and cost.  Contains methods to get and set each of these parameters as well as a toString()
    // method that will return these parameters in a readable string
    //--------------------------------------------------------------------------------------------------
    class Automobile
    {
    	public:
    
    		// Returns a string with column headings that match up with the string returned from
    		// toString()
    		string formatHeader();
    
    		// Returns the cost of the car
    		float getCost();
    
    		// Returns the Id of the car
    		string getId();
    
    		// Returns the make of the car
    		string getMake();
    
    		// Returns the mileage of the car
    		int getMileage();
    
    		// Returns the model of the car
    		string getModel();
    
    		// Sets the car's unique Id number
    		void setId(string argId);
    
    		// Sets the make of the car
    		void setMake(string argMake);
    
    		// Sets the model of the car
    		void setModel(string argModel);
    
    		// Sets the mileage of the car
    		void setMileage(int argMileage);
    		
    		// Sets the cost of the car
    		void setCost(float argCost);
    
    		// Converts the car's parameters into a readable string
    		string toString();
    
    	private:
    
    		// The unique identification number for the car
    		string mId;
    
    		// The make of the car
    		string mMake;
    
    		// The model of the car
    		string mModel;
    
    		// The mileage of the car
    		int mMileage;
    
    		// The cost of the car
    		float mCost;
    }
    
    #endif
    This one doesn't require it:
    Code:
    //--------------------------------------------------------------------------------------------------
    // Include Directive
    //--------------------------------------------------------------------------------------------------
    #ifndef TOKENIZER_H
    #define TOKENIZER_H
    
    #include <string>
    using namespace std;
    
    //--------------------------------------------------------------------------------------------------
    // A class that will take strings and tokenize them
    //--------------------------------------------------------------------------------------------------
    class Tokenizer
    {
    	public:
    
    		// Tokenizes the passed string into an array of strings using the passed character as the
    		// delimiter between tokens
    		int tokenize(string str, string tokens[], char delim);
    }
    
    #endif
    I picked two header files whose include directives are as similar as possible because this is what has baffled me the most. The include directives are nearly identical in these files as you can see.
    Last edited by wwfarch; 05-12-2004 at 12:35 AM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That seems like an odd problem. You should probably avoid the using directive an header files, so maybe try putting std:: before everything that requires it instead of using namespace std.

    Code:
    class Automobile
    {
    public:
    
    // Returns a string with column headings that match up with the string returned from
    // toString()
    std::string formatHeader();
    
    //...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Whats the error that you get if you do not put the ; in front of using?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Your classes must end with a semi-colon. Yours do not.

    Code:
    class FooBar
    {
      // blah blah
    }; // <--- Right there buddy...
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Heh I didn't even look for that. My advice still stands, though.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    6
    When you use #define, aren't you required to use two parms?

    For instance...

    #define macro mylongfunctionname();

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No you don't have to

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    3

    It's fixed

    MrWizard: I've added the semicolon at the end of each class in my project and it fixed the error.

    JaWib: Is there any particular reason that I shouldn't have an include directive in my header files? Just curious as to your thought pattern behind this advice.

    I'd like to thank everyone for the quick and informative feedback that they gave me. I came primarily from a Java background so I have a tendency to forget some of the slight variations in the c++ syntax from Java.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 04:10 PM
  2. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 12:30 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 02:38 PM
  5. namespace std
    By strobe9 in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 09:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21