将 header 中定义的 MyClass 作为函数参数传递给其他文件
Passing MyClass defined in header as function argument to other file
我已经花了大约一个小时,但在网上找不到任何有用的信息。问题是我有一些文件,例如 a.h、b.h、a.cpp、b.cpp 和 main.cpp。在 a.h 中,我声明了一个容器,其中包含我自己定义的属性。我想将此容器作为参数传递给 b.h/b.cpp 中的函数。这样做的方法是什么?
a.h 文件
struct Container{
int size;
int* array
...};
b.cpp
void someFunction(Container container)
{...}
感谢您的帮助。
使用#include "file"
来包含您需要的文件。
例如 b.cpp
:
#include "a.h"
void someFunction(Container container);
您还应该将 include guards 放入您的头文件中。
它们可以防止不必要的多次包含同一文件。如果您的文件名为 a.h
,您将编写以下内容:
a.h
:
#ifndef A_H
#define A_H
// ... your code ...
#endif // A_H
在 b.h 中,您应该输入 #include "a.h"
以便容器描述可用。之后,您可以简单地声明您的功能,就像您在问题中拥有它们一样。所以你的文件看起来像这样:
a.h
#ifndef A_H
#define A_H
struct Container{
int size;
int* array
...};
#endif // A_H
b.h
#ifndef B_H
#define B_H
#include "a.h"
void someFunction(Container container);
#endif // B_H
b.cpp
void someFunction(Container container)
{ ... }
我已经花了大约一个小时,但在网上找不到任何有用的信息。问题是我有一些文件,例如 a.h、b.h、a.cpp、b.cpp 和 main.cpp。在 a.h 中,我声明了一个容器,其中包含我自己定义的属性。我想将此容器作为参数传递给 b.h/b.cpp 中的函数。这样做的方法是什么?
a.h 文件
struct Container{
int size;
int* array
...};
b.cpp
void someFunction(Container container)
{...}
感谢您的帮助。
使用#include "file"
来包含您需要的文件。
例如 b.cpp
:
#include "a.h"
void someFunction(Container container);
您还应该将 include guards 放入您的头文件中。
它们可以防止不必要的多次包含同一文件。如果您的文件名为 a.h
,您将编写以下内容:
a.h
:
#ifndef A_H
#define A_H
// ... your code ...
#endif // A_H
在 b.h 中,您应该输入 #include "a.h"
以便容器描述可用。之后,您可以简单地声明您的功能,就像您在问题中拥有它们一样。所以你的文件看起来像这样:
a.h
#ifndef A_H
#define A_H
struct Container{
int size;
int* array
...};
#endif // A_H
b.h
#ifndef B_H
#define B_H
#include "a.h"
void someFunction(Container container);
#endif // B_H
b.cpp
void someFunction(Container container)
{ ... }