C++ 在头文件中使用未声明的标识符 (Clang)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26835902/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:40:49  来源:igfitidea点击:

Use of undeclared identifier in header file (Clang)

c++functionshared-librariesclangheader-files

提问by Axmill

I am creating a function to read the contents of a file, located in an IO.cpp file:

我正在创建一个函数来读取位于 IO.cpp 文件中的文件的内容:

#include "IO.h"
#include <iostream>
#include <fstream>
IO::IO()
{
    //ctor
}

void IO::readFile(std::string fileName)
{
    std::ofstream inputFile;
    inputFile.open(FileName);
    inputFile >> fileName.toStdString;
    inputFile.close();
    std::cout << fileName;
}

With the header file IO.h:

使用头文件 IO.h:

#ifndef IO_H
#define IO_H


class IO
{
    public:
        IO();
        void readFile(std::string inputFile);
    protected:
    private:
};

#endif // IO_H

But I get an error from Clang that says

但是我从 Clang 收到一个错误,说

include/IO.h|9|error: use of undeclared identifier 'std'|

include/IO.h|9|错误:使用未声明的标识符“std”|

And I can't figure out how to solve it.

我不知道如何解决它。

回答by Luchian Grigore

When parsing the header (specifically the void readFile(std::string inputFile);line), the compiler doesn't know an stdnamespace exists, much less stringexists inside that namespace.

在解析标头(特别是void readFile(std::string inputFile);行)时,编译器不知道std命名空间存在,更不用说string存在于该命名空间内。

#include <string>in the header.

#include <string>在标题中。