我收到阻止我运行程序的警告,但不确定如何解决

I'm getting a warning that is preventing me from running my program and I'm not sure how to fix it

本文关键字:何解决 不确定 解决 阻止我 运行 程序 警告      更新时间:2023-10-16

该代码应该从文件中读取输入并将该信息放入哈希表中,如果出现任何碰撞,它也应该处理这些信息。我不确定如何修复我使用该行 int count = 0 的错误,这是错误警告:

non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

..

#include <iostream>
#include<stdlib.h>
#include<string.h>
#include<map> 
#include <ctime>
#include<bits/stdc++.h>
using namespace std ;
struct record{
        char last_name[15] ;
        char first_name[15] ;
        unsigned int account_num ;
        unsigned int month ;
        unsigned int day ;
        unsigned int year ;
        float annual_salary ;
        char dept_code ; 
        char phone_num[12] ;
};
struct Node{
        record p ;
        struct Node * next ;
};
struct Hash{
        struct Node * head ;
        int count = 0 ;
};
Hash H[37] ;
/* Given a reference (pointer to pointer) to the head
   of a list and an int, appends a new node at the end  */
void append(struct Node** head_ref,record new_data)
{
    /* 1. allocate node */
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    struct Node *last = *head_ref;  /* used in step 5*/
    /* 2. put in the data  */
    new_node->p  = new_data;
    /* 3. This new node is going to be the last node, so make next of
          it as NULL*/
    new_node->next = NULL;
    /* 4. If the Linked List is empty, then make the new node as head */
    if (*head_ref == NULL)
    {
       *head_ref = new_node;
       return;
    }
    /* 5. Else traverse till the last node */
    while (last->next != NULL)
        last = last->next;
    /* 6. Change the next of last node */
    last->next = new_node;
    return;
}
void insert(record r)
{
        int x = r.account_num %37 ;
        append(&(H[x].head),r) ;
}
map<int,string> m ;
void printdata()
{
        time_t now = time(0);
        char* dt = ctime(&now) ;
        int z = strlen(dt) ;
        int yea = 0 , temp = 1;
        z--;
        while(dt[z]!=' ')
        {
                yea += temp*(dt[z]-'0');
                temp = temp*10 ;
                z--;
        }
        int mont ;
        char ch[3] ;
        ch[0] = dt[4] ;
        ch[1] = dt[5] ;
        ch[2] = dt[6] ;
        for(int i=1;i<=12;i++)
        {
                if(m[i]==ch)
                {
                        mont = i ;
                        break ;
                }
        }
        m[1] = "Jan" ;
        m[2] = "Feb" ;
        m[3] = "Mar" ;
        m[4] = "Apr" ;
        m[5] = "May" ;
        m[6] = "Jun" ;
        m[7] = "Jul" ;
        m[8] = "Aug" ;
        m[9] = "Sep" ;
        m[10] = "Oct" ;
        m[11] = "Nov" ;
        m[12] = "Dec" ;
        struct Node * q ;
        cout << "LAST Name       Acct Number     date of birth            annual salary          department code       agen";
        for(int i=0;i<=36;i++)
        {
                q = (H[i].head);
                int cnt = 0 ;
                while(q!=NULL)
                {
                        cnt++;
                        q = q->next ;
                }
                temp = q->p.year - yea + (q->p.month - mont>=6 ? 1:0) ;
                q = H[i].head ; 
                cout << q->p.last_name <<" "<<q->p.account_num ;
                if(cnt>1)cout<<"*";
                cout <<" "<<m[q->p.month]<<". "<<q->p.day<<","<<q->p.year<<" "<<q->p.annual_salary<<" "<<q->p.dept_code<<" "<<temp<<"n" ;;
                if(q==NULL)continue ;
                q = q->next ;
                while(q!=NULL)
                {
                        temp = q->p.year - yea + (q->p.month - mont>=6 ? 1:0) ;
                        cout << q->p.last_name <<" "<<q->p.account_num ;
                    cout <<" "<<m[q->p.month]<<". "<<q->p.day<<","<<q->p.year<<" "<<q->p.annual_salary<<" "<<q->p.dept_code<<" "<<temp<<"n" ;;
                   q = q->next ;
                }
        }
        return ;
}
int main()
{
        record r ;
        freopen("data.txt","r",stdin);
        while(cin >> r.last_name)
        {
             cin >> r.first_name >> r.account_num >> r.month >> r.day >> r.year >> r.annual_salary >> r.dept_code ;
             if(cin >> r.phone_num) ;
             insert(r) ;
        }
        return 0 ;
}

错误消息是自称的。像您在代码的第27行中所做的那样,不允许您在结构声明中初始化非静态数据成员。相反,您可以:

  1. 定义Hash struct的构造函数,并在C-TOR中初始化count成员。
  2. 在实例化Hash结构对象后,每次初始化计数成员。我不建议使用此解决方案,尤其是如果您坚持在全球范围内宣布Hash H[37]数组(我也不建议,这是另一个问题。(。

从点1的意义上。上面,Hash类声明看起来像这样:

struct Hash{
    Hash() :
        head(NULL),
        count(0)
    {}
    struct Node * head ;
    int count;
};