Is there a way to get the line the code is being executed on?
For example:
int death() {
cout << "An error has occurred on line " << line << endl;
exit(128);
}
Then if I call death() function on line 64 it tells me that the error occurred on line 64.
EDIT: Most people seem to be telling me to use the __LINE__ macro. I know you can do that:
#define DEATH(message) death(__LINE__, message)
where
int death(int line, string message) {
cout << "An error has occurred at line << line << endl;
}
But I'm thinking of using it in such a way:
string readFile(string file) {
string str;
ifstream stream(file);
if (!stream) { death(); } //if this is line 23, it prints line 23
stream >> str;
stream.close();
return str;
}
//instead of printing line 64 if readFile() is executed on line 64
And using the __LINE__ macro alone only returns the line at which the death() function was executed. In other words, if I execute readFile() at line 64, I want death() to print line 64, not the line at which it was defined inside the readFile() function.
Aucun commentaire:
Enregistrer un commentaire