【问题标题】:Use of undeclared identifier in header file (Clang)在头文件中使用未声明的标识符 (Clang)
【发布时间】:2015-01-06 06:58:28
【问题描述】:

我正在创建一个函数来读取位于 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;
}

带头文件IO.h:

#ifndef IO_H
#define IO_H


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

#endif // IO_H

但是我从 Clang 收到一个错误提示

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

我不知道如何解决它。

【问题讨论】:

  • 你没有在 IO.h 中包含

标签: c++ function shared-libraries clang header-files


【解决方案1】:

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

#include &lt;string&gt; 在标题中。

【讨论】:

    【解决方案2】:

    在许多情况下,当您忘记在单独文件上的方法实现时包含 #include &lt;iostream&gt; 时会发生这种情况,即在主函数之外(即当您转到头文件路径时)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多