为什么我必须在'using'语句之前添加额外的令牌?

时间:2012-02-08 06:15:36

标签: c++ compilation token

Functions.h

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <limits>
#include <stdexcept>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "definitions.h"
#include "globals.h"

};using namespace std;

这是functions.h现在我们main.cpp包含标题

Main.cpp的

#include "functions.h"

int main(int argc, char *argv[])
{
    //Other stuff
    return 0;
}

出于某种原因,我必须在};声明之前写using。它不会让我编译,除非它在它之前。

关于为什么的任何想法?

1 个答案:

答案 0 :(得分:5)

此错误可能是由};文件中缺少globals.h引起的。

预处理器将#include d头文件的内容直接粘贴到源文件中。因此,如果其中一个头文件中存在语法错误,则源文件中将包含语法错误。

我知道这不能解决你的问题,但是你不应该在头文件中使用using namespace std;,因为这会污染包含该头的每个翻译单元的全局命名空间。将using namespace语句保留在单个源文件的本地更好,或者更好的是,只需键入std::

相关问题