【问题标题】:I need help with properly initlizing a structure and passing it into a function properly我需要帮助正确初始化结构并将其正确传递给函数
【发布时间】:2011-04-29 23:07:05
【问题描述】:

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

错误如下;

project5.cpp(145) : error C2676: binary '[' : 'Data' 未定义此运算符或转换为预定义运算符可接受的类型

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

project5.cpp(146) : error C2676: binary '[' : 'Data' 未定义此运算符或转换为预定义运算符可接受的类型

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

project5.cpp(147) : error C2676: binary '[' : 'Data' 未定义此运算符或转换为预定义运算符可接受的类型

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

project5.cpp(148) : error C2676: binary '[' : 'Data' 未定义此运算符或转换为预定义运算符可接受的类型

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

project5.cpp(149) : error C2676: binary '[' : 'Data' 未定义此运算符或转换为预定义运算符可接受的类型

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

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,而它的大小为 100 的 NCAAStats,但我不确定我做错了什么这是造成这种情况的原因,任何建议将不胜感激。

【问题讨论】:

    标签: c++ visual-studio-2008 structure


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      注意这个错误:

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

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

      【讨论】:

        【解决方案3】:

        您已在全局范围内声明了一个数组 NCAAStats[Size]。但是,您的函数具有称为 NCAAStats 的参数;这些隐藏全局定义。因此,像NCAAStats[Idx] 这样的东西是无效的,因为NCAAStatsData &amp;,而不是Data[]

        【讨论】:

        • 非常感谢您的帮助,解决了问题。
        猜你喜欢
        • 1970-01-01
        • 2020-02-29
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        相关资源
        最近更新 更多