18

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Axmill
  • 305
  • 1
  • 2
  • 6

2 Answers2

27

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

In many cases this occurs when you forget to include #include <iostream> on implementation of methods on a separate file i.e. outside main function (i.e. when you go header file route)