I'm in a course called intro to C++ for C programmers, so I already have a basic grasp of C programming and am a few weeks into C++. We have to work with files and have an assignment that gives us a main() program. We have to use the instructions to provide the definitions for the functions so the main() program works, by creating a header and a .cpp implementation. Standard stuff.
Anyhow, I commented out all the stuff in the main() except the stuff about opening and closing a file. There's a function there whos prototype is
FILE* open(const char filename[]);
In other words, the main program is calling this function with similar syntax to this.
open("file.txt");
I define it in my .cpp as
FILE* open(const char filename[])
{
FILE *fp;
fp=fopen(const *filename, "r");
return fp;
}
For reference, here are all my includes in the cpp file. The .h file has just the prototypes and a struct variable (which I haven't used yet).
here is the compile time error:
g++ main.cpp ISBNPrefix.cpp
In file included from main.cpp:14:
ISBNPrefix.h:14: error: expected initializer before '*' token
main.cpp: In function 'int main()':
main.cpp:27: error: 'open' was not declared in this scope
main.cpp: At global scope:
main.cpp:84: error: expected unqualified-id before 'return'
main.cpp:85: error: expected declaration before '}' token
In file included from ISBNPrefix.cpp:11:
ISBNPrefix.h:14: error: expected initializer before '*' token
ISBNPrefix.cpp: In function 'FILE* open(const char*)':
ISBNPrefix.cpp:17: error: expected primary-expression before 'const'
Any more info or anything that needs to be clarified, please let me know! Halp!
Cheers, did that. Edited my code to include new compile errors, return fp not *fp, and
#include <cstdio>
Update?
I should add that in the compile errors, the line that references line 14 in ISBNPrefix.h is referring to the prototype for that function in the header.
EDIT
Ok, I took away the "const *" from the fopen() call. That helped a bit
Also, I was an idiot in the way I commented out the code in the main() file. Fixed that.
Now looks like:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#include "ISBNPrefix.h"
FILE* open(const char filename[])
{
FILE* fp;
fp=fopen(filename, "r");
return fp;
}
New compile errors:
g++ main.cpp ISBNPrefix.cpp
In file included from main.cpp:14:
ISBNPrefix.h:14: error: expected initializer before '*' token
main.cpp: In function 'int main()':
main.cpp:27: error: 'open' was not declared in this scope
In file included from ISBNPrefix.cpp:11:
ISBNPrefix.h:14: error: expected initializer before '*' token
The compile error in main() is because open() is not declared. It should be declared in my .cpp file, but I can't get it to recognize that it needs to return a FILE..... variable? Type? whatever, naming conventions are the least of my concerns right now
hans, The instructions specify to use it. It's trying to transition us from C to C++ and we're still in the early stages. The focus of the assignment is on modules and teaching the proper syntax for using FILEs in C++ (fstream and the like) is beyond the scope of this assignment
Zhuge: Forehead slap. I'll do that and see what occurs
EDIT
It worked! You're a genius! Full marks! I name today Zhuge Month!