views:

380

answers:

4

I've been trying to include a structure called "student" in a student.h file, but I'm not quite sure how to do it.

My student.h file code consists of entirely:

#include<string>
using namespace std;

struct Student;

while the student.cpp file consists of entirely:

#include<string>
using namespace std;

struct Student {
    string lastName, firstName;
    //long list of other strings... just strings though
};

Unfortunately, files that use #include "student.h" come up with numerous errors like error C2027: use of undefined type 'Student', error C2079: 'newStudent' uses undefined struct 'Student' (where newStudent is a function with a Student parameter), and error C2228: left of '.lastName' must have class/struct/union. It appears the compiler (VC++) does not recognize struct Student from "student.h" or something?

I have tried declaring the whole struct in "student.h", but it didn't help either. How can I declare struct Student in "student.h" so that I can just #include "student.h" and start using the struct? BTW, it seems there are no compiler errors in student.h...

+2  A: 

You've only got a forward declaration for student in the header file; you need to place the struct declaration in the header file, not the .cpp. The method definitions will be in the .cpp (assuming you have any).

andand
+4  A: 

Your student.h file only forward declares a struct named "Student", it does not define one. This is sufficient if you only refer to it through reference or pointer. However, as soon as you try to use it (including creating one) you will need the full definition of the structure.

In short, move your struct Student { ... }; into the .h file and use the .cpp file for implementation of member functions (which it has none so you don't need a .cpp file).

Noah Roberts
+4  A: 

You should not place an using directive in an header file, it can give you problems in future.

Also you need an include guard in your header.

EDIT: of course, after having fixed the include guard issue, you also need a complete declaration of student in the header file. As pointed out by others the forward declaration is not sufficient in your case.

baol
Thanks for telling about the include guard... that fixed it. lol :)
wrongusername
-1. Although those are both true statements, I don't see how either of them would address the error messages reported in the question.
Rob Kennedy
@rob It wasn't an answer meant to be accepted ;) I just thought he needed the two advices. I believe that my answer, combined with the others, regarding the forward declaration, solved the problem.
baol
A: 

Try this new source :

student.h :

#include<string>

using namespace std;

struct Student {
    string lastName, firstName;
    //long list of other strings... just strings though
};

student.cpp :

#include "student.h"

struct Student s1;
Zai
-1 for posting an answer that doesn't compile. "string" in student.h should be "std::string".-1 for lack of explanation. Feed a man a fish ...
Rob Adams
Thanks for the correction !! I'm a newcomer here..
Zai