gpt4 book ai didi

c++ - 我需要帮助正确初始化结构并将其正确传递给函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:58:41 25 4
gpt4 key购买 nike

我正在编写一个简单的 C++ 数据库,使用 Visual Studio 2008 Express Edition,例如按年份对包含 NCAA 优胜者和亚军的文本文件进行排序和搜索的程序,这些数据将保存在一个结构中。我了解如何进行排序和搜索,但我在正确初始化结构并将其传递给函数时遇到了问题,因为当我尝试这样做时,我在类里面的表现如何,我遇到了几个我无法得到的错误摆脱,任何帮助将不胜感激。

报错如下;

project5.cpp(145) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator

project5.cpp(145) : error C2228: left of '.year' must have class/struct/union

project5.cpp(146) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator

project5.cpp(146) : error C2228: left of '.schoolWin' must have class/struct/union

project5.cpp(147) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator

project5.cpp(147) : error C2228: left of '.scoreWin' must have class/struct/union

project5.cpp(148) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator

project5.cpp(148) : error C2228: left of '.schoolRunnerUp' must have class/struct/union

project5.cpp(149) : error C2676: binary '[' : 'Data' does not define this operator or a conversion to a type acceptable to the predefined operator

project5.cpp(149) : error C2228: left of '.scoreRunnerUp' must have class/struct/union

project5.cpp(191) : error C2664: 'initializeStructure' : cannot convert parameter 1 from 'Data [100]' to '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 认为我的结构被命名为数据,大小为 100,而它是大小为 100 的 NCAAStats,但我不确定我做错了什么这是造成这种情况的原因,我们将不胜感激任何建议。

最佳答案

既然你标记了 C++,我建议你使用 C++ 特性...

#include <vector>
#include <string>
struct Data
{
unsigned int year;
std::string schoolWin;
unsigned int scoreWin;
std::string schoolRunnerUp;
unsigned int scoreRunnerUp;

Data()
: year(0)
, schoolWin("winning school")
, scoreWin(0)
, schoolRunnerUp("loosing school")
, scoreRunnerUp(0){}
}

// In main
std::vector<Data> my_data(100); // Create and initialize 100 instances of "Data".

关于c++ - 我需要帮助正确初始化结构并将其正确传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5838250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com