In 14+ years of programming I have never even seen an attempt to declare a class type within a cpp file as shown by the OP |
I don't suppose the "OP" stands for what in Britain is called an OAP, an Old Age Pensioner ;-).
My problem could of course well be related to my age: trumping your "14+ years," I last seriously looked at the standard for C in the early '90s when there wasn't even a standard for C++, and when the (Unix) C++Compiler had got neither working templates nor exceptions, and nobody had even heard of
namespace.
I am, however, surprised that you hadn't come across this before. I felt it was only reasonable in a program that reads and parses data from the standard input and later reads records from a database and parses those, to use loops of the kind
1 2 3 4 5 6 7
|
class entry;
main() {
entry line;
while (line.read(cin))
cout << line;
}
|
and, in a different file,
1 2 3 4 5 6 7
|
class entry;
/* ... */
entry* ep;
for (ep = mydb.first(); ep; mydb.next()) {
/* do something with *ep */
}
|
and have
class entry be locally defined to implement the details of the format expected from reading stdin or the database, respectively. None of these details are needed outside the respective source files. A classic case for name-spaces, of course, but surely not unusual.
One could, and perhaps should, adopt as a programming standard that
all type definitions must be in header files and that at least one of the source files includes
all header files as a matter of principle. Then the compiler would indeed issue an error.