Пытаюсь перегрузить оператор <<, уже много туториалов посмотрел, все время выдает ошибки по типу passing 'const Lexer' as 'this' argument of 'std::map<int, std::basic_string<char> > Lexer::read_file()' discards qualifiers [-fpermissive]
, no match for 'operator<<' in 'stream << Lexer::read_file()()'
, no match for 'operator<<' in 'std::cout << outtokens'
. Код:
#include <iostream>
#include <fstream>
#include <map>
#include <ostream>
using std::map;
class Lexer {
public:
map <int, std::string> tokens;
std::string content;
std::ifstream file;
int line = 1;
~Lexer() {
file.close();
}
map <int, std::string> read_file() {
file.open("code.kspl");
if (!file) {
std::cout << "Cannot open file" << std::endl;
}
else {
while (!file.eof()) {
getline(file, content);
if (content.find("out") == 0) {
content.erase(0, 5);
content.erase(content.length()-1, 1);
tokens[line] = "PRINT " + content;
}
line+=1;
//std::cout << content << std::endl;
}
return tokens;
}
}
};
class Parser {
public:
map <std::string, int> intvars;
map <std::string, float> floatvars;
map <std::string, std::string> strvars;
map <std::string, bool> boolvars;
bool isError = false;
~Parser() {
if (isError) {
std::cout << "[Program finished with error]";
}
else {
std::cout << "[Program finished with exit status OK]";
}
}
};
std::ostream& operator<<(std::ostream& stream, const Lexer& lexer) {
stream << lexer.read_file();
return stream;
}
int main() {
Lexer lexer;
map <int, std::string> outtokens = lexer.read_file();
std::cout << outtokens << std::endl;
return 0;
}
Как исправить?