我需要帮助正确初始化结构并将其正确传递到函数中

发布于 2024-11-04 11:52:17 字数 2319 浏览 2 评论 0原文

我正在编写一个简单的 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 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 &'

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 技术交流群。

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

发布评论

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

评论(3

生寂 2024-11-11 11:52:17

既然你标记了 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".

Since you tagged C++ I'd suggest you use C++ features...

#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".
唔猫 2024-11-11 11:52:17

请注意此错误:

project5.cpp(191) : error C2664: 'initializeStructure' : cannot convert parameter 1 from 'Data [100]' to 'Data &'

您为initializeStructure()定义的参数NCAAStats是对NCAAStats类型的一个结构的引用,您不能像数组一样使用它(我假设这就是您想要的)。您必须将其更改为指针Data *Data[])以匹配您实际作为争论。

Note this error:

project5.cpp(191) : error C2664: 'initializeStructure' : cannot convert parameter 1 from 'Data [100]' to 'Data &'

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 * or Data[]) to match the type you're actually passing as an argument.

岁月如刀 2024-11-11 11:52:17

您已在全局范围内声明了一个数组 NCAAStats[Size]。但是,您的函数具有名为 NCAAStats 的参数;这些隐藏全局定义。因此,诸如 NCAAStats[Idx] 之类的内容无效,因为 NCAAStatsData &,而不是 Data[]< /代码>。

You have declared an array NCAAStats[Size] at the global scope. However, your functions have arguments called NCAAStats; these hide the global definition. Therefore, stuff like NCAAStats[Idx] is invalid, because that NCAAStats is a Data &, not a Data[].

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