C++ MS VC 中的错误 C2143(缺少;在命名空间之前)包括
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4309532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error C2143 (missing ; before namespace) in MS VC include
提问by Russell
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!
Time.h 是一个非常简单的类,它在类描述的末尾有一个分号,所以我很确定我的代码在这里是正确的..然后我得到了同样的错误:Microsoft Visual Studio 10.0\VC\include \内存.. 任何想法!?!?谢谢!
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
时间.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)
DepartureTime.h(继承类)
#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
回答by OJ.
This can happen when one include file has an error (or missing semicolon) at the endof 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.
当一个包含文件具有在一个错误(或缺少分号),就会发生这种情况到底该文件和该文件之前,另一个立刻就包含。生成的错误表明第二个包含文件而不是第一个包含问题。因此,作为起点,请确保,如果您有一个包含在 Time.h 之前的文件,则该文件中没有任何错误。
Pasting the content of your .h and .cpp files would certainly be helpful rather than just the error message.
粘贴 .h 和 .cpp 文件的内容肯定会有所帮助,而不仅仅是错误消息。
回答by Leo Davidson
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.
在 Time.h 之前 Stop.cpp 包括什么?查看该文件的末尾,您可能会看到问题出在哪里。
回答by Wakkawakka
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!
我遇到了同样的问题,但我发现在我的代码中,这个错误的原因是我忘记在我的一个类的类声明之后放一个分号。尝试查看您的文件以确保您有一个,并且最好在所有文件的末尾添加一个分号。为我工作!
回答by Amfasis
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.
这已经是很久以前的事了,但我也有同样的错误(至少是数字),使用 Qt 和 MSVC 并且可以解决它,所以这是我的贡献。
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)
我设法通过使用编译器标志 /P 找到了罪魁祸首。这将生成带有标题的文件。通过搜索错误指向的代码,我可以发现预处理器实际上替换了一个模板参数(我有一个命名错误的#define)
回答by Mitch
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.
在回到我多年未接触的项目并第一次使用 MSVC2015 之后,我遇到了同样的错误。我认为要么我的环境已经过时,要么库没有经过编译器的测试。
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. :)
始终检查git log
您是否要回到旧项目。:)
回答by qaisjp
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:
如果执行合并,请尝试:
- Figure out if there's a particular class causing the issue
- 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.
- 确定是否存在导致问题的特定类
- 请参阅旧代码以查看该文件中是否有一行引用了不再出现在新代码中的那个类。