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
Use of undeclared identifier in header file (Clang)
提问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 std
namespace exists, much less string
exists inside that namespace.
在解析标头(特别是void readFile(std::string inputFile);
行)时,编译器不知道std
命名空间存在,更不用说string
存在于该命名空间内。
#include <string>
in the header.
#include <string>
在标题中。