我需要帮助正确初始化结构并将其正确传递到函数中
我正在编写一个简单的 C++ 数据库,使用 Visual Studio 2008 Express 版本,例如按年份对包含 NCAA 冠军和亚军的文本文件进行排序和搜索的程序,该数据将保存在一个结构中。我了解如何进行排序和搜索,但我在正确初始化结构并将其传递到函数中时遇到了麻烦,因为当我尝试按照课堂上的显示方式进行操作时,我遇到了一些我无法得到的错误摆脱,任何帮助将不胜感激。
错误如下;
project5.cpp(145) : 错误 C2676: 二进制
'['
:'Data'
未定义此运算符或转换为预定义可接受的类型运算符project5.cpp(145) : 错误 C2228:
'.year'
的左侧必须具有类/结构/联合project5.cpp(146) : 错误 C2676: 二进制
'['
:'Data'
未定义此运算符或转换为预定义可接受的类型运算符project5.cpp(146) : 错误 C2228:
'.schoolWin'
的左侧必须具有类/结构/联合project5.cpp(147) : 错误 C2676: 二进制
'['
:'Data'
未定义此运算符或转换为预定义可接受的类型运算符project5.cpp(147) : 错误 C2228:
'.scoreWin'
的左侧必须具有类/结构/联合project5.cpp(148) : 错误 C2676: 二进制
'['
:'Data'
未定义此运算符或转换为预定义可接受的类型运算符project5.cpp(148) : 错误 C2228:
'.schoolRunnerUp'
的左侧必须具有类/结构/联合project5.cpp(149) : 错误 C2676: 二进制
'['
:'Data'
未定义此运算符或转换为预定义可接受的类型运算符project5.cpp(149) : 错误 C2228:
'.scoreRunnerUp'
的左侧必须具有类/结构/联合project5.cpp(191):错误 C2664:
'initializeStructure'
:无法将参数 1 从'Data [100]'
转换为'Data & '
我的结构声明:
const int Size = 100; // size of structure
struct Data { // Struct definintion to take in stats
int year;
string schoolWin;
int scoreWin;
string schoolRunnerUp;
int scoreRunnerUp;
} NCAAStats [Size] ;
void initializeStructure(Data& NCAAStats, int Size); // initialization function prototype
//function declaration
void initializeStructure(Data& NCAAStats, int Size) {
int Idx;
for (Idx = 0; Idx < Size; Idx++) {
NCAAStats[Idx].year = 0000;//line145
NCAAStats[Idx].schoolWin = "winning school";//line146
NCAAStats[Idx].scoreWin = 000;//line147
NCAAStats[Idx].schoolRunnerUp = "losing school";//line148
NCAAStats[Idx].scoreRunnerUp = 000;//line149
}
}
//initalize the array of structures
initializeStructure(NCAAStats, Size);//line 191 function call from within main
基于最后一个错误,我认为可能由于某种原因 Visual Studio 认为我的结构名为 Data当大小为 100 时,NCAAStats 的大小为 100,但我不确定我做错了什么导致了这种情况,任何建议将不胜感激。
I am writing a simple C++ database, using Visual Studio 2008 express edition, like program to sort and search a text file containing NCAA winners and runner ups by year, this data is to be saved within a structure. I understand how to do the sorting and searching but i am having trouble with properly initializing the structure and passing it into a function, as when i try to do it how i was shown in class i get several errors that i have been unable to get rid of, any help would be greatly appreciated.
The errors are as follows;
project5.cpp(145) : error C2676: binary
'['
:'Data'
does not define this operator or a conversion to a type acceptable to the predefined operatorproject5.cpp(145) : error C2228: left of
'.year'
must have class/struct/unionproject5.cpp(146) : error C2676: binary
'['
:'Data'
does not define this operator or a conversion to a type acceptable to the predefined operatorproject5.cpp(146) : error C2228: left of
'.schoolWin'
must have class/struct/unionproject5.cpp(147) : error C2676: binary
'['
:'Data'
does not define this operator or a conversion to a type acceptable to the predefined operatorproject5.cpp(147) : error C2228: left of
'.scoreWin'
must have class/struct/unionproject5.cpp(148) : error C2676: binary
'['
:'Data'
does not define this operator or a conversion to a type acceptable to the predefined operatorproject5.cpp(148) : error C2228: left of
'.schoolRunnerUp'
must have class/struct/unionproject5.cpp(149) : error C2676: binary
'['
:'Data'
does not define this operator or a conversion to a type acceptable to the predefined operatorproject5.cpp(149) : error C2228: left of
'.scoreRunnerUp'
must have class/struct/unionproject5.cpp(191) : error C2664:
'initializeStructure'
: cannot convert parameter 1 from'Data [100]'
to'Data &'
My Structure Declaration:
const int Size = 100; // size of structure
struct Data { // Struct definintion to take in stats
int year;
string schoolWin;
int scoreWin;
string schoolRunnerUp;
int scoreRunnerUp;
} NCAAStats [Size] ;
void initializeStructure(Data& NCAAStats, int Size); // initialization function prototype
//function declaration
void initializeStructure(Data& NCAAStats, int Size) {
int Idx;
for (Idx = 0; Idx < Size; Idx++) {
NCAAStats[Idx].year = 0000;//line145
NCAAStats[Idx].schoolWin = "winning school";//line146
NCAAStats[Idx].scoreWin = 000;//line147
NCAAStats[Idx].schoolRunnerUp = "losing school";//line148
NCAAStats[Idx].scoreRunnerUp = 000;//line149
}
}
//initalize the array of structures
initializeStructure(NCAAStats, Size);//line 191 function call from within main
Based off the last error i was thinking that it is possible that for some reason visual studio thinks my structure is named Data with a size of 100 when, it is NCAAStats of size 100, but i am not sure what i did wrong that is causing this, any suggestions would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然你标记了 C++,我建议你使用 C++ 功能......
Since you tagged C++ I'd suggest you use C++ features...
请注意此错误:
您为initializeStructure()定义的参数NCAAStats是对NCAAStats类型的一个结构的引用,您不能像数组一样使用它(我假设这就是您想要的)。您必须将其更改为指针(
Data *
或Data[]
)以匹配您实际作为争论。Note this error:
The argument NCAAStats you defined for initializeStructure() is a reference to one struct of type NCAAStats, which you can't use like an array (I assume that's what you were going for). You'll have to change it to a pointer (
Data *
orData[]
) to match the type you're actually passing as an argument.您已在全局范围内声明了一个数组
NCAAStats[Size]
。但是,您的函数具有名为 NCAAStats 的参数;这些隐藏全局定义。因此,诸如NCAAStats[Idx]
之类的内容无效,因为NCAAStats
是Data &
,而不是Data[]< /代码>。
You have declared an array
NCAAStats[Size]
at the global scope. However, your functions have arguments calledNCAAStats
; these hide the global definition. Therefore, stuff likeNCAAStats[Idx]
is invalid, because thatNCAAStats
is aData &
, not aData[]
.