This is the header file File.h:
#include "common.h"
#ifndef FILE_H
#define FILE_H
using namespace std;
class File : public fstream{
public:
string file;
File(){
File("/tmp/text");
}
File(string file_name): file(file_name), fstream(file_name.c_str(), fstream::in | fstream::out){
}
void read_line();
};
#endif
And this is the File.cpp file:
#include "File.h"
void File::read_line(){
string line;
while(getline(*this, line)){
cout << "line: " << line << endl;
}
}
And following is the content of common.h
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stack>
#include <unistd.h>
#include <cassert>
#include <stdexcept>
#include <getopt.h>
#include <fstream>
#include <istream>
When compiling above code, I get below error:
/home/rohit/cpp/cmake_projs/tree/src/File.cpp: In member function ‘void File::read_line()’:
/home/rohit/cpp/cmake_projs/tree/src/File.cpp:7:24: error: no matching function for call to ‘File::getline(File&, std::__cxx11::string&)’
while(getline(*f, line)){
^
In file included from /usr/include/c++/7/iostream:40:0,
from /home/rohit/cpp/cmake_projs/tree/include/common.h:1,
from /home/rohit/cpp/cmake_projs/tree/include/File.h:1,
from /home/rohit/cpp/cmake_projs/tree/src/File.cpp:1:
But the code compiles fine when I put almost same code in a different function not related to File class:
void test(){
string line;
File *f = new File("/tmp/abc");
while(getline(*f, line)){
cout << "line: " << line << endl;
}
}
Why is this standard function hidden in the class function read_line()
and not in an independent function?