Search code examples
c++functionshared-librariesclangheader-files

Use of undeclared identifier in header file (Clang)


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

#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:

#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

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

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


Solution

  • 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.

    #include <string> in the header.