C++,如何在头文件中声明一个结构体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2732978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++, how to declare a struct in a header file
提问by wrongusername
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.
我一直试图在student.h
文件中包含一个名为“student”的结构,但我不太确定如何去做。
My student.h
file code consists of entirely:
我的student.h
文件代码完全由:
#include<string>
using namespace std;
struct Student;
while the student.cpp
file consists of entirely:
而该student.cpp
文件完全由:
#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
不幸的是,使用的文件#include "student.h"
会出现许多错误,例如
error C2027: use of undefined type 'Student'
error C2079: 'newStudent' uses undefined struct 'Student' (where newStudent is a function with a `Student` parameter)
error C2228: left of '.lastName' must have class/struct/union
It appears the compiler (VC++) does not recognize struct Student from "student.h"?
编译器(VC++)似乎无法识别“student.h”中的 struct Student?
How can I declare struct Student in "student.h" so that I can just #include "student.h" and start using the struct?
如何在“student.h”中声明 struct Student 以便我可以 #include “student.h” 并开始使用该结构?
采纳答案by baol
You should not place an using
directive in an header file, it creates unnecessary headaches.
您不应该using
在头文件中放置指令,它会造成不必要的麻烦。
Also you need an include guardin 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.
编辑:当然,在修复了包含保护问题之后,您还需要在头文件中完整声明 student 。正如其他人所指出的,在您的情况下,前向声明是不够的。
回答by Zai
Try this new source :
试试这个新来源:
student.h
学生.h
#include <iostream>
struct Student {
std::string lastName;
std::string firstName;
};
student.cpp
学生.cpp
#include "student.h"
struct Student student;
回答by Eric Leschinski
C++, how to declare a struct in a header file:
C++,如何在头文件中声明一个结构体:
Put this in a file called main.cpp:
把它放在一个名为 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
把它放在一个名为 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;
.
您不应using namespace std;
在 C++ 头文件中放置指令,因为您可能会导致不同库之间的无提示名称冲突。要解决此问题,请使用完全限定名称: std::string foobarstring;
而不是将 std 命名空间包含在string foobarstring;
.
回答by Edward Strange
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.
您的 student.h 文件仅向前声明了一个名为“Student”的结构,它没有定义一个。如果您仅通过引用或指针引用它,这就足够了。但是,一旦您尝试使用它(包括创建一个),您将需要结构的完整定义。
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).
简而言之,移动你的 struct Student { ... }; 进入 .h 文件并使用 .cpp 文件来实现成员函数(它没有,所以你不需要 .cpp 文件)。
回答by andand
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).
您student
在头文件中只有一个前向声明;您需要将结构声明放在头文件中,而不是 .cpp。方法定义将在 .cpp 中(假设您有任何)。
回答by Jordan LaPrise
Okay so three big things I noticed
好吧,我注意到三件大事
You need to include the header file in your class file
Never, EVER place a using directive inside of a header or class, rather do something like std::cout << "say stuff";
Structs are completely defined within a header, structs are essentially classes that default to public
您需要在类文件中包含头文件
永远不要在头文件或类中放置 using 指令,而是执行 std::cout << "say stuff"; 之类的操作。
结构完全定义在头文件中,结构本质上是默认为公共的类
Hope this helps!
希望这可以帮助!
回答by AnT
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.
为了“使用”该结构,即能够声明该类型的对象并访问其内部结构,您需要该结构的完整定义。因此,如果您想要执行任何操作(并且根据您的错误消息判断),您必须将结构类型的完整定义放入头文件中。