C++, how to declare a struct in a header file

后端 未结 7 1496
暖寄归人
暖寄归人 2021-01-30 14:40

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 c

相关标签:
7条回答
  • 2021-01-30 14:43

    C++, how to declare a struct in a header file:

    Put this in a file called main.cpp:

    #include <cstdlib>
    #include <iostream>
    #include "student.h"
    
    using namespace std;    //Watchout for clashes between std and other libraries
    
    int main(int argc, char** argv) {
        struct Student s1;
        s1.firstName = "fred"; s1.lastName = "flintstone";
        cout << s1.firstName << " " << s1.lastName << endl;
        return 0;
    }
    

    put this in a file named student.h

    #ifndef STUDENT_H
    #define STUDENT_H
    
    #include<string>
    struct Student {
        std::string lastName, firstName;
    };
    
    #endif
    

    Compile it and run it, it should produce this output:

    s1.firstName = "fred";
    

    Protip:

    You should not place a using namespace std; directive in the C++ header file because you may cause silent name clashes between different libraries. To remedy this, use the fully qualified name: std::string foobarstring; instead of including the std namespace with string foobarstring;.

    0 讨论(0)
  • 2021-01-30 14:48

    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).

    0 讨论(0)
  • 2021-01-30 14:52

    Try this new source :

    student.h

    #include <iostream>
    
    struct Student {
        std::string lastName;
        std::string firstName;
    };
    

    student.cpp

    #include "student.h"
    
    struct Student student;
    
    0 讨论(0)
  • 2021-01-30 14:55

    Okay so three big things I noticed

    1. You need to include the header file in your class file

    2. Never, EVER place a using directive inside of a header or class, rather do something like std::cout << "say stuff";

    3. Structs are completely defined within a header, structs are essentially classes that default to public

    Hope this helps!

    0 讨论(0)
  • 2021-01-30 15:02

    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).

    0 讨论(0)
  • 2021-01-30 15:03

    You can't.

    In order to "use" the struct, i.e. to be able to declare objects of that type and to access its internals you need the full definition of the struct. So, it you want to do any of that (and you do, judging by your error messages), you have to place the full definition of the struct type into the header file.

    0 讨论(0)
提交回复
热议问题