无法将结构传递给另一个文件中声明的功能?
Can't pass struct to function declared in another file?
我有一系列变量,包括在另一个文件(Vek.h(中声明的结构。在这个文件中,我还声明了一个名为correct
:的函数
class Vek: public Player{
protected :
int counter = 0;
struct next {int nexti=0; int nextj=0;};
next newNext = {0,0};
int jTemp;
int iTemp = 0;
int jPast;
int iPast;
int hits;
int pastHits = 0;
int count = 0;
void putShip(Cell[OCEAN_SIZE][OCEAN_SIZE]);
int correct(int, int, int, int, int, int, struct);
// int fire(vector<int>submit);
Coords shot;
在第二个文件(Vek.cpp(中,我尝试调用此函数并将结构体next
作为参数传递:
int Vek :: correct(int iTemp, int jTemp, int iPast, int jPast, int hits, int count, struct newNext){
int attemptsi[2]{iTemp+1,iTemp-1};
int attemptsj[2]{jTemp+1,jTemp-1};
int i;
int j;
if(count == 0){
i = attemptsi[0];
j = jTemp;
// Coordinates.j = 5;
}else if(count == 1){
i = attemptsi[1];
j = jTemp;
// Coordinates.j = 5;
}else if(count == 2){
i = iTemp;
j = attemptsj[0];
// Coordinates.j = 5;
}else if(count == 3){
i = iTemp;
j = attemptsj[1];
// Coordinates.j = 5;
}
next.nexti = i;
next.nextj = j;
count++;
return next;
}
由于结构的原因,我在调用correct
时出错。在其他人中,我得到了错误:
"correct"的越界定义与任何"Vek"中的声明
这里发生了什么?如何将结构体next
传递给correct
函数?
如果correct
周围没有空格,我会得到错误:
匿名结构的声明必须是定义
结构的定义由组成
- 关键字
struct
- 通常是结构的(类型(名称
- 结构体,大括号中
示例:
struct Int_Struct { int mI; };
^ ^ ^
keyword name body
名称(此处为Int_Struct
(可以直接用作类型名称(与C不同(。
该类型变量的定义遵循通常的约定typename variablename
:
Int_Struct myVar;
一如既往,对于函数参数的声明也是如此:
void f(Int_Struct myParam);
您可以在原型中,或者如果没有使用参数,则省略参数名称,只需编写void f(Int_Struct);
。但是典型地;说";给出了参数名称,以便读者了解参数的内容;最好的文档是自文档。
关键字struct
不能用作类型名称,就像在函数定义int Vek :: correct(int iTemp, int jTemp, int iPast, int jPast, int hits, int count,
structnewNext)
中对最后一个参数所做的那样:记住,参数声明的形式为typename parametername
,这使得struct
成为类型名称,正如所述,这是不可能的。如果您希望在参数或变量声明中以C方式冗余地使用关键字struct作为类型名称的部分:struct Int_Struct varName;
相当于更惯用和常见的Int_Struct varName;
。
- .cpp和.h文件中的模板专用化声明
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 用"=default"声明的函数应该只在头文件中执行
- 调用在 HXX 文件中声明的静态数据成员
- 如何在 c++ 的不同文件中声明类?
- 如何在头文件中声明类模板(由于循环依赖关系)
- 声明要在 qml 和 cpp 文件中使用的全局字符串
- 在.cpp文件之间传递结构声明,而不使用全局头文件
- 为什么要将函数声明和定义放在单独的文件中
- 枚举前向声明与头文件
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- 在带有类型保护的模板文件中使用前向声明
- 在指向现有内存地址的 hpp 文件中声明成员函数的最佳方法
- 在多个头文件中从接口声明被覆盖的函数时,如何避免重复代码?
- 应该在类中的文件声明为静态
- 是否应从头文件声明中排除辅助函数?(C++)
- 哪个C++头文件声明 strlen
- 如何为头文件声明“#include”以避免“error lnk2005”
- 打开文件和关闭文件声明定位:最佳做法,优点,缺点
- 说双向文件声明不隐式设置标志是否正确