我知道这是一个非常荒谬的问题,但这很令人困惑和烦恼,因为一些应该工作的东西不是.我正在使用GCC编译器的代码块,我试图在我的类中简单地创建一个字符串变量
#ifndef ALIEN_LANGUAGE
#define ALIEN_LANGUAGE
#include <string>
class Language
{
    public:
    private:
        string str;
};
#endif
奇怪的是,我的编译器停止了我的错误说:
C:\Documents and Settings\...|11|error: `string' does not name a type|
||=== Build finished: 1 errors, 0 warnings ===|
由于某种原因,它无法找到类"字符串",由于某种原因,我的main.cpp能够检测到"#include"而我的语言类由于某种原因无法使用.
这是我快速编写的主要内容,只是为了看到它本身能够看到字符串文件:
//main.cpp
#include <iostream>
#include <string>
#include "alien_language.h"
using namespace std;
int main()
{
    string str;
    return 0;
}
有谁知道发生了什么?
字符串类在std命名空间中定义.你应该把课程改为:
class Language
{
    public:
    private:
        std::string str;
};
也可以,但不建议将其添加到头文件的顶部:
using namespace std;
字符串在命名空间std中,您需要在头文件中完全限定它:
#include <string>
class Language
{
    public:
    private:
        std::string str;
};
不要using namespace std;在头文件中使用或类似文件。