C++,如何在头文件中声明结构体

发布于 2024-08-31 07:56:02 字数 884 浏览 3 评论 0原文

我一直在尝试在 student.h 文件中包含一个名为“student”的结构,但我不太确定该怎么做。

我的 student.h 文件代码完全由:

#include<string>
using namespace std;

struct Student;

student.cpp 文件完全由:

#include<string>
using namespace std;

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

不幸的是,使用 #include "student.h"< 的文件/code> 出现许多错误,例如

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 

编译器 (VC++) 无法识别“student.h”中的 struct Student?

如何在“student.h”中声明 struct Student,以便我可以 #include“student.h” 并开始使用该结构?

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)

error C2228: left of '.lastName' must have class/struct/union 

It appears the compiler (VC++) does not recognize struct Student from "student.h"?

How can I declare struct Student in "student.h" so that I can just #include "student.h" and start using the struct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

美煞众生 2024-09-07 07:56:02

尝试这个新源:

student.h

#include <iostream>

struct Student {
    std::string lastName;
    std::string firstName;
};

Student.cpp

#include "student.h"

struct Student student;

Try this new source :

student.h

#include <iostream>

struct Student {
    std::string lastName;
    std::string firstName;
};

student.cpp

#include "student.h"

struct Student student;
热情消退 2024-09-07 07:56:02

您不应在头文件中放置 using 指令,它会创建 不必要的麻烦

此外,您的标头中还需要一个 包含防护

编辑:当然,修复了包含保护问题后,您还需要在头文件中完整声明学生。正如其他人指出的那样,前向声明对于您的情况来说是不够的。

You should not place an using directive in an header file, it creates unnecessary headaches.

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.

好菇凉咱不稀罕他 2024-09-07 07:56:02

C++,如何在头文件中声明结构体:

将其放入名为 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;
}

将其放入名为 Student.h 的文件中

#ifndef STUDENT_H
#define STUDENT_H

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

#endif

编译并运行它,它应该产生以下输出:

s1.firstName = "fred";

Protip:

您不应在 C++ 头文件中放置 using namespace std; 指令,因为这可能会导致不同库之间发生静默名称冲突。要解决此问题,请使用完全限定名称:std::string foobarstring;,而不是使用 string foobarstring; 包含 std 命名空间。

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

只涨不跌 2024-09-07 07:56:02

您的 Student.h 文件仅向前声明了一个名为“Student”的结构,它没有定义一个。如果您仅通过引用或指针引用它,这就足够了。然而,一旦您尝试使用它(包括创建一个),您将需要该结构的完整定义。

简而言之,移动你的 struct Student { ... };进入 .h 文件并使用 .cpp 文件来实现成员函数(它没有成员函数,因此您不需要 .cpp 文件)。

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

寂寞笑我太脆弱 2024-09-07 07:56:02

您在头文件中只有 student 的前向声明;您需要将结构声明放在头文件中,而不是 .cpp 中。方法定义将位于 .cpp 中(假设您有)。

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

别低头,皇冠会掉 2024-09-07 07:56:02

好吧,我注意到三件大事

  1. 你需要在类文件中包含头文件

  2. 永远不要在头文件中放置 using 指令或类,而是做类似 std::cout << 的事情“say stuff”;

  3. 结构完全在标头中定义,结构本质上是默认为 public 的类

希望这有帮助!

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!

不顾 2024-09-07 07:56:02

你不能。

为了“使用”该结构,即能够声明该类型的对象并访问其内部,您需要该结构的完整定义。因此,如果您想要执行其中任何操作(根据错误消息判断,您确实这样做了),则必须将结构类型的完整定义放入头文件中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文