开发者

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 speci开发者_JAVA技巧fier - 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


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.


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.


I had the same problem, but I found that in my code the reason for this error was that I forgot to put a semicolon after the class declarations of one of my classes. Try looking through your files to make sure you have one, and for good measure just chuck a semicolon at the end of all your files. Worked for me!


It's already a long time ago, but I also had the same error (at least the number), with Qt and MSVC and could solve it, so this is my contribution.

I managed to find the culprit by using the compiler flag /P. This generates the file with headers in place. By searching for the code the error pointed at I could find that the preprocessor actually replaced a template argument (I had a badly named #define)


After coming back to a project that I hadn't touched in ages and using MSVC2015 with it for the first time, I got the same kind of errors. I thought that either my environment was outdated or the libraries hadn't been tested with the compiler.

Eventually I noticed that the last commit I wrote before taking a months-long break from the project broke the build by adding

namespace Foo {

to every header, without bothering to add the closing bracket.

Always check git log if you're coming back to an old project. :)


I ran into this issue when doing a big merge.

I made a mistake and accidentally missed out a forward declaration at the top of one of my header files:

class CTrainTrack;

Adding this line fixed everything!

If performing a merge, try to:

  1. Figure out if there's a particular class causing the issue
  2. Refer to the old code to see if there's a line in that file that refers to that class which is no longer in the new code.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜