Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 01-27-2005, 01:04 AM   #1
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
C++: What's wrong with this header file?

Code:
/***** common.h *****/ void open_database(ofstream &out);

Everything works if I don't pass anything to the function otherwise the compiler screams with:

Code:
In file included from recordsub.cpp:4: common.h:3: `ofstream' was not declared in this scope common.h:3: `out' was not declared in this scope common.h:3: variable or field `open_database' declared void recordsub.cpp: In function `int main()': recordsub.cpp:24: `open_database' cannot be used as a function
Code:
/***** common.cpp *****/ #include iostream #include fstream #include "common.h" using namespace std; void open_database(ofstream &out) { out.open ("submissions", ios::binary); if (out.fail()) { cout << "Error: The database did not open..." << endl; } return; }
Code:
/***** recordsub.cpp *****/ #include cstdlib #include fstream #include iostream #include "common.h" #define MAX_NAME_LEN 50 using namespace std; struct Record { char name[MAX_NAME_LEN + 1]; //citizen tax data int sin; double income; streampos left; //links to left and right subtrees streampos right; }; int main() { ofstream out; open_database(out); out.close(); return 0; }
I thought it might have been something wrong with my makefile but it works as long as I don't include any arguments in the header file. I figure it's something pretty simple but do you think I can see it?


Last edited by Karim : 01-27-2005 at 01:05 AM.
Karim is offline   Reply With Quote
Old 01-27-2005, 01:43 AM   #2
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
Shouldn't the includes for iostream and fstream be in the header file instead of the .cpp file? If you don't put them in the header, then the compiler doesn't know what ofsteam is for the parameter type. At least, I don't think it would.
sabotai is offline   Reply With Quote
Old 01-27-2005, 02:18 AM   #3
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Thanks, sab. The document I was working from only had simple examples so none of the sample header files had any #includes.
Karim is offline   Reply With Quote
Old 01-27-2005, 05:49 AM   #4
Marc Vaughan
SI Games
 
Join Date: Oct 2000
Location: Melbourne, FL
One thing to bear in mind for the future which might help with compile speeds if you work on large projects is 'forward declarations of classes'.

If a class is referenced in a header but its content isn't required for compilation (generally speaking if you're using pointers to it in the class functions or data) then you can forward declare a class.

This is simply telling the compiler "don't panic the class exists and the linker will find it" and allows you to avoid having unrequired includes in header files.

To do this use the syntax:

class MY_CLASS;

This indicates that a class called MY_CLASS exists and not to worry about its contents.

(please note this is a basic overview of how 'forward declarations work' and isn't meant to be 100% correct in low level stuff to do with compilers)
Marc Vaughan is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 11:47 AM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.