Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 377
Axmill Avatar asked Nov 10 '14 02:11

Axmill


People also ask

What is use of undeclared identifier?

The identifier is undeclaredIf the identifier is a variable or a function name, you must declare it before it can be used. A function declaration must also include the types of its parameters before the function can be used.

What does use of undeclared identifier mean in C++?

The identifier is undeclared: In any programming language, all variables have to be declared before they are used. If you try to use the name of a such that hasn't been declared yet, an “undeclared identifier” compile-error will occur. Example: #include <stdio.h> int main()


1 Answers

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.

like image 128
Luchian Grigore Avatar answered Sep 25 '22 21:09

Luchian Grigore