Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error C2143 (missing ; before namespace) in MS VC include

I am extremely confused why I am getting this strange error all the sudden:

Time.h is a very simple class, and it has a semicolon at the end of the class description, so I am pretty sure my code is correct here.. Then I get the same errors in: Microsoft Visual Studio 10.0\VC\include\memory.. Any ideas!?!? Thanks!

Compiler Output

1>ClCompile:
1>  Stop.cpp
1>c:\projectnextbus\Time.h(17): error C2143: syntax error : missing ';' before 'using'
1>c:\projectnextbus\Time.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>  NextBusDriver.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory(16): error C2143: syntax error : missing ';' before 'namespace'
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Update: Can't really post all the code as this is for a school project and we aren't supposed to post before we submit, but small snippets should be ok..

Time.h

#ifndef TIME_HPP
#define TIME_HPP

#include <string>
#include <sstream>

using namespace std;

class Time {
// Defines a time in a 24 hour clock

public:
    // Time constructor
    Time(int hours = 0 , int minutes= 0);

    // POST: Set hours int
    void setHours(int h);

    // POST: Set minutes int
    void setMinutes(int m);

    // POST: Returns hours int
    int getHours();

    // POST: Returns minutes int
    int getMinutes();

    // POST: Returns human readable string describing the time
    // This method can be overridden in inheriting classes, so should be virtual so pointers will work as desired
    string toString();

private: 
    string intToString(int num);
    // POST: Converts int to string type

    int hours_;
    int minutes_;

};

#endif

DepartureTime.h (inherited class)

#ifndef DEPARTURE_TIME_HPP
#define DEPARTURE_TIME_HPP

#include <string>
#include "Time.h"

using namespace std;

class DepartureTime: public Time {
public:
    // Departure Time constructor
    DepartureTime(string headsign, int hours=0, int minutes=0) : Time(hours, minutes), headsign_(headsign) { }

    // POST: Returns bus headsign
    string getHeadsign();

    // POST: Sets the bus headsign
    void setHeadsign(string headsign);

    // POST: Returns human readable string describing the departure
    string toString();

private:
    // Class variables
    string headsign_;
};
#endif
like image 657
Russell Avatar asked Nov 30 '10 00:11

Russell


2 Answers

This can happen when one include file has an error (or missing semicolon) at the end of the file and that file is included immediately before another. The error that is generated indicates that there is a problem in the second include file rather than the first. So as a starting point, make sure that, if you have a file that's included before Time.h, that is doesn't have any errors in it.

Pasting the content of your .h and .cpp files would certainly be helpful rather than just the error message.

like image 87
OJ. Avatar answered Nov 05 '22 09:11

OJ.


This is almost always caused by a missing ; in the header BEFORE the one where the error is reported.

What does Stop.cpp include before Time.h? Look at the end of that file and you'll probably see where the problem is.

like image 43
Leo Davidson Avatar answered Nov 05 '22 09:11

Leo Davidson